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


#1

Originally published at: https://www.cyberciti.biz/faq/bash-check-if-process-is-running-or-notonlinuxunix/

I am new to Linux shell scripting. I am using a CentOS 7 Linux and Ubuntu 18.04 LTS server in the cloud. How do I check if a process is running or not on Linux? How do I determine whether a process is running or not in a shell script running on an Ubuntu server?


#2
systemctl status <process name>

for example: systemctl status sshd


#3

Depends on the OP’s definition of process. If they mean something started by the system, then yes, this answer is correct. If they just mean “something which might be running, started by either the system or the user”, then you’ll have to go to the ‘ps’ command.

ps aux | grep process_name
ps auxww | grep process_name
ps -deaf | grep process_name
ps -deafll | grep process_name

Depending on your system and how long the command is, one of the above should work. You’ll have to check the status of the output.

PROCESS="named"
ps -deaf | grep "${PROCESS}" > /dev/null 2>&1
RETVAL="$?"
case "${RETVAL}" in
  0) echo "${PROCESS} is running" ;;
  *) echo "${PROCESS} is not running" ;;
esac