Bash check if process is running or not on Linux / Unix

www.‮al‬utturi.com
Bash check if process is running or not on Linux / Unix

In Bash, you can check if a process is running or not using the ps command and the grep command. The ps command is a command-line utility that is used to display information about the currently running processes on your system. The grep command is a command-line utility that is used to search for patterns in text.

To check if a process is running, you can use the ps command to list all of the running processes on your system, and then use the grep command to search for the process that you want to check. For example, to check if the Apache HTTP Server process is running, you can use the following command:

ps -aux | grep apache2

This will list all of the running processes on your system, and then use the grep command to search for any processes with the name "apache2". If the Apache HTTP Server process is running, you should see one or more lines of output that contain the process name and other information about the process.

If the process is not running, you will not see any output from the grep command. In this case, you can use the echo command to print a message indicating that the process is not running. For example, you could use the following command to print a message if the Apache HTTP Server process is not running:

if ! ps -aux | grep apache2; then
    echo "Apache is not running"
fi

This will check if the Apache HTTP Server process is running, and if it is not running, it will print the message "Apache is not running".

Remember that the ps and grep commands are part of the standard Bash shell, so they should be available on most Linux and UNIX systems. You can use these commands in a Bash script or at the command prompt to check if a process is running or not.

Created Time:2017-10-16 14:38:39  Author:lautturi