This is a utility script used to list out the processes running in the unix / linux (OS). This helps to find out which processes are running and which are not with a single command.

This uses the process name along with the path and using that, from the ps list, greps the respective process and then finds the pid is running or not. It is configurable to any number of other processes you want to list out.

Listing-1: unix bash script

#!/bin/bash
#

#Location of JAVA_HOME (bin files)
export JAVA_HOME=/usr/java/jdk1.7.0_67

# location of the process to be checked out
export PROCESS_NAME_REDIS=redis
export PROCESS_NAME_TOMCAT=/home/mysuer/apps/apache-tomcat
export PROCESS_NAME_ELASTICSEARCH=/home/myuser/apps/elasticsearch


get_pid_redis() {
       # returns the process id of the redis process
       echo `ps -fe | grep $REDIS_PROCESS | grep -v grep | tr -s " "|cut -d" " -f2`
}

get_pid_tomcat() {
       echo `ps -fe | grep $TOMCAT_PROCESS | grep -v grep | tr -s " "|cut -d" " -f2`
}

get_pid_elastic() {
       echo `ps -fe | grep $ES_PROCESS | grep -v grep | tr -s " "|cut -d" " -f2`
}


list_process_status(){

        # check if redis server is running or not
        pid=$(get_pid_redis)
        if [ -n "$pid" ]; then 
           echo -e "\e[00;32mRedis is running with pid: $pid\e[00m"
        else 
           echo -e "\e[00;31mRedis is not running\e[00m"
        fi


        # check if elasticsearch server is running or not
        pid=$(get_pid_elastic)

        if [ -n "$pid" ]; then 
          echo -e "\e[00;32mElastic Search is running with pid: $pid\e[00m"
        else 
          echo -e "\e[00;31m Elastic search is not running\e[00m"
        fi
        

        # check if the tomcat process is running or not
        pid=$(get_pid_tomcat)
        if [ -n "$pid" ]; then 
          echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
        else 
          echo -e "\e[00;31mTomcat is not running\e[00m"
        fi            
}



# call function to get the process list
list_process_status


Output format

The output lists out the process in either red or green color along with the process id. If the process is running then its displayed in green color because of the text inside the "\e[00;32 .. any text in between goes green \e[00m" and the process if is not running then its displayed in red color because of the text inside the "\e[00;31m ..any text in between goes red...\e[00m”.