Linux test if process is running

Linux/Unix command to determine if process is running?

I need a platform independent (Linux/Unix|OSX) shell/bash command that will determine if a specific process is running. e.g. mysqld , httpd . What is the simplest way/command to do this?

15 Answers 15

While pidof and pgrep are great tools for determining what’s running, they are both, unfortunately, unavailable on some operating systems. A definite fail safe would be to use the following: ps cax | grep command

The output on Gentoo Linux:

14484 ? S 0:00 apache2 14667 ? S 0:00 apache2 19620 ? Sl 0:00 apache2 21132 ? Ss 0:04 apache2

The output on OS X:

42582 ?? Z 0:00.00 (smbclient) 46529 ?? Z 0:00.00 (smbclient) 46539 ?? Z 0:00.00 (smbclient) 46547 ?? Z 0:00.00 (smbclient) 46586 ?? Z 0:00.00 (smbclient) 46594 ?? Z 0:00.00 (smbclient)

On both Linux and OS X, grep returns an exit code so it’s easy to check if the process was found or not:

#!/bin/bash ps cax | grep httpd > /dev/null if [ $? -eq 0 ]; then echo "Process is running." else echo "Process is not running." fi 

Furthermore, if you would like the list of PIDs, you could easily grep for those as well:

ps cax | grep httpd | grep -o '^[ ]*7*'

Whose output is the same on Linux and OS X:

The output of the following is an empty string, making this approach safe for processes that are not running:

echo ps cax | grep aasdfasdf | grep -o '^[ ]*8*'

This approach is suitable for writing a simple empty string test, then even iterating through the discovered PIDs.

#!/bin/bash PROCESS=$1 PIDS=`ps cax | grep $PROCESS | grep -o '^[ ]*1*'` if [ -z "$PIDS" ]; then echo "Process not running." 1>&2 exit 1 else for PID in $PIDS; do echo $PID done fi 

You can test it by saving it to a file (named «running») with execute permissions (chmod +x running) and executing it with a parameter: ./running «httpd»

#!/bin/bash ps cax | grep httpd if [ $? -eq 0 ]; then echo "Process is running." else echo "Process is not running." fi 

Please keep in mind that you’re simply parsing the output of ps ax which means that, as seen in the Linux output, it is not simply matching on processes, but also the arguments passed to that program. I highly recommend being as specific as possible when using this method (e.g. ./running «mysql» will also match ‘mysqld’ processes). I highly recommend using which to check against a full path where possible.

Читайте также:  Ecryptfs mount private linux

The process can be running, but stopped. So if the goal is to test if mysqld or httpd are «up and running» (responding), you should also check if it is stopped or not.

Sorry, but while the answer is certainly right from a semantic point of view I’m fully against trying to find a process by pattern matching on the process arg vector. Any such approach is doomed to fail sooner or later (you actually admit to that yourself, by saying that more checks are needed). I’ve added my own recommendation in a separate answer.

grep will also find itself running (e.g. ps cax | grep randomname will always return 0 because grep finds grep randomname (hope this is clear. ). One fix is to add square brackets around the first letter of the process name, e.g. ps cax | grep [r]andomname .

ps cax may not output command name wholly. E.g it prints «chromium-browse» instead of «chromium-browser».

You SHOULD know the PID !

Finding a process by trying to do some kind of pattern recognition on the process arguments (like pgrep «mysqld» ) is a strategy that is doomed to fail sooner or later. What if you have two mysqld running? Forget that approach. You MAY get it right temporarily and it MAY work for a year or two but then something happens that you haven’t thought about.

Only the process id (pid) is truly unique.

Always store the pid when you launch something in the background. In Bash this can be done with the $! Bash variable. You will save yourself SO much trouble by doing so.

Читайте также:  Latest nvidia driver linux mint

How to determine if process is running (by pid)

So now the question becomes how to know if a pid is running.

This is POSIX and hence portable. It will return the pid itself if the process is running or return nothing if the process is not running. Strictly speaking the command will return a single column, the pid , but since we’ve given that an empty title header (the stuff immediately preceding the equals sign) and this is the only column requested then the ps command will not use header at all. Which is what we want because it makes parsing easier.

This will work on Linux, BSD, Solaris, etc.

Another strategy would be to test on the exit value from the above ps command. It should be zero if the process is running and non-zero if it isn’t. The POSIX spec says that ps must exit >0 if an error has occurred but it is unclear to me what constitutes ‘an error’. Therefore I’m not personally using that strategy although I’m pretty sure it will work as well on all Unix/Linux platforms.

Источник

How to check the process is already running or not

I want to check the particular process in already running or not. I refereed this Q&A. But I didn’t get any specific solution. Following is the example that I tried: I have created abc.sh file and run this script on background, like sh abc.sh & . Now this file is running on background and I fire the ps aux | grep «abc» command. Following is the output of this command:

prakash 3594 0.0 0.0 4388 820 pts/0 S+ 16:44 0:00 grep --color=auto abc 

After that I stop the abc.sh running script and fire the same command ps aux | grep «abc» command. But I am getting same output like:

prakash 3594 0.0 0.0 4388 820 pts/0 S+ 16:44 0:00 grep --color=auto abc 

7 Answers 7

Every process will be listed in the output of ps aux ; whether running, sleeping, zombie or stopped.

Читайте также:  What is linux next

However, in your case, since you ran the process using sh abc.sh , sh is the application(shell) that is running and not abc.sh . Hence, ps aux will not contain the process abc.sh because of which grep could not yield any result.

So, the correct way you should have used it is as:

This may also return you other process that are running having the string sh anywhere in their output of ps aux .

You should note that the process will be «running» when the output of ps aux has its STAT as R . If it is something other than that, it is not running at the instance you fired the command to check the running processes. The different process states can be found in the man page for ps:

D uninterruptible sleep (usually IO) R running or runnable (on run queue) S interruptible sleep (waiting for an event to complete) T stopped, either by a job control signal or because it is being traced W paging (not valid since the 2.6.xx kernel) X dead (should never be seen) Z defunct ("zombie") process, terminated but not reaped by its parent 

You could as well run the top command to check if the process is running or sleeping and the amount of CPU, RAM it is consuming. (This will again list your process as sh ).

However, if you do want your process to be listed as abc.sh , then you should have the first line of the script you are running as:

so that the shell will know what application to use to run the script(sh in this case, change it to #!/bin/bash for bash) and then provide executable permissions to the process using:

replacing /path/to/ with the location of the abc.sh file and then run abc.sh using

again replacing /path/to/ with the location of the abc.sh file.

Источник

Оцените статью
Adblock
detector