Linux process running status

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 '^[ ]*9*'

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 '^[ ]*7*'

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 '^[ ]*4*'` 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.

Читайте также:  Pirate games on 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.

Читайте также:  Linux mint windows mbr

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.

Источник

Status of process on linux

Please show some reasonable intent in solving this question, include some sample code, and remember that Stack Overflow is not a code writing service.

1 Answer 1

First of all, there are more process states than «Running», «Done» and «Stopped», from man ps:

PROCESS STATE CODES Here are the different values that the s, stat and state output specifiers (header "STAT" or "S") will display to describe the state of a process: D uninterruptible sleep (usually IO) R running or runnable (on run queue) S interruptible sleep (waiting for an event to complete) T stopped by job control signal t stopped by debugger during the tracing 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 can get the status of one process by its pid (process ID) using the command ps:

Читайте также:  Чем открыть tiff linux

From man ps:

-q pidlist Select by PID (quick mode). This selects the processes whose process ID numbers appear in pidlist. With this option ps reads the necessary info only for the pids listed in the pidlist and doesn't apply additional filtering rules. The order of pids is unsorted and preserved. No additional selection options, sorting and forest type listings are allowed in this mode. Identical to q and --quick-pid. 

If you have the array of pid «All_Process_Pid» in your bash script, then you can just:

for i in "$" do echo "process $i has the state $(ps -q $i -o state --no-headers)" done 

Источник

How to Understand Linux Process Statuses

In our “How to use ps” article we talked about the Linux command ps and how it can be used to examine the currently running processes on a Linux server.

When using ps with the “u” flag (ps -u) you will see a column called STAT that displays the process’s status.

Here is a list of the various process statuses and what they mean:

D – Uninterruptible sleep (usually a critical system process, a process that cannot be killed without rebooting)
R – Running or runable (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.
Z – Defunct (“zombie”) process, terminated but not closed by the parent process that created it

Additional characters may be seen if in a BSD environment or when using the “stat” modifier with ps:

W – has no resident pages
– high-priority process
N – low-priority task
L – has pages locked into memory (for real-time and custom IO)

Liquid Web’s Heroic Support is always available to assist customers with this or any other issue. If you need our assistance please contact us:
Toll Free 1.800.580.4985
International 517.322.0434
support@liquidweb.com
https://my.liquidweb.com/

Источник

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