110

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?

Highway of Life
  • 22,803
  • 16
  • 52
  • 80

15 Answers15

184

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

Whose output is the same on Linux and OS X:

3519 3521 3523 3524

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

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

WARNING!!!

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.


References:

http://linux.about.com/od/commands/l/blcmdl1_ps.htm

http://linux.about.com/od/commands/l/blcmdl1_grep.htm

Caleb Gray
  • 3,040
  • 2
  • 21
  • 32
  • 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. – oluc May 02 '13 at 08:16
  • 2
    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. – peterh Oct 03 '13 at 10:41
  • 6
    `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`. – Kyle G. Oct 29 '13 at 03:19
  • `ps cax | rev | cut -f1 -d' ' | rev` will show only the name column, for easier parsing. – Tyzoid May 21 '15 at 14:56
  • 1
    `ps cax` may not output command name wholly. E.g it prints "chromium-browse" instead of "chromium-browser". – jarno Jun 18 '15 at 22:24
  • @KyleG. `ps cax | grep randomname` grep will not return 0 always, but `ps ax | grep randomname` will. – jarno Jun 19 '15 at 10:24
  • @jarno, you're absolutely right, my bad. I should've tested my suggestion first ;) – Kyle G. Jun 19 '15 at 20:45
31

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.

How to determine if process is running (by pid)

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

Simply do:

ps -o pid= -p <pid>

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.

peterh
  • 18,404
  • 12
  • 87
  • 115
  • 1
    Except this doesn't answer the question, which is to determine if a service is running. The PID won't be known in such cases, therefore this answer is only valid if you *do* know the PID. – Highway of Life Nov 14 '13 at 22:56
  • 2
    Wrong. My whole point of the comment is to take a step back and say that if you first find yourself in the situation where you have to do some form of `grep ` to find a given process then you have done something wrong when you started the process, IMHO. I take it from the OP's question that indeed he has control over how the process is started. – peterh Nov 16 '13 at 09:40
  • 2
    The more correct "term" for the OP question should have been "cross-platform command to determine if a service is running", it's not the same system running the check, but an outside system, so the PID is just not going to be known at all. – Highway of Life Nov 18 '13 at 17:42
  • Whether or not the PID will be known depends on if you control the startup process or not. It doesn't depend on from where you do the check. You can implement your own 'is-service-running?' (which uses a stored PID) on a given host which can then be called from a remote host. But again: if you do not control the startup process of the service then I understand your point. – peterh Nov 20 '13 at 14:46
  • I do, but the scripts are independent of each-other. That is why `ps cax | grep ` ended up being a very reliable and simple solution. – Highway of Life Nov 20 '13 at 22:15
  • How to determine if process is running (by pid) is exactly what I was looking for. I was surprised that all of the questions in my search results were about how to find the pid. – ShadSterling Jan 20 '16 at 22:33
  • 2
    This isn't foolproof. The process you're interested in may have died after the system has been up long enough for PIDs to wrap around, and another process may then have been allocated the same PID you're checking for. http://stackoverflow.com/questions/11323410/linux-pid-recycling – claymation Apr 15 '16 at 03:28
  • 1
    @claymation. Fair point. However PID method is still better than pattern matching on process args as PID clash is far more unlikely than say accidentally starting two instances of the same service. Just my two cents. :-) – peterh Apr 15 '16 at 05:54
15

On most Linux distributions, you can use pidof(8).

It will print the process ids of all running instances of specified processes, or nothing if there are no instances running.

For instance, on my system (I have four instances of bashand one instance of remmina running):

$ pidof bash remmina
6148 6147 6144 5603 21598

On other Unices, pgrep or a combination of ps and grep will achieve the same thing, as others have rightfully pointed out.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
8

This should work on most flavours of Unix, BSD and Linux:

PATH=/usr/ucb:${PATH} ps aux | grep httpd | grep -v grep

Tested on:

  • SunOS 5.10 [Hence the PATH=...]
  • Linux 2.6.32 (CentOS)
  • Linux 3.0.0 (Ubuntu)
  • Darwin 11.2.0
  • FreeBSD 9.0-STABLE
  • Red Hat Enterprise Linux ES release 4
  • Red Hat Enterprise Linux Server release 5
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • 3
    +1 Yes simply `ps`. To avoid the second `grep` I suggest: `ps aux | grep [h]ttpd` – oHo Feb 02 '12 at 18:19
  • I didn't use the square bracket trick here to make it easier to put a variable into the main `grep`. – johnsyweb Feb 02 '12 at 18:28
  • 1
    All right ;) I have just tested on Red Hat AS 4 and Red Hat AP 5. Of course I works! So you can add in your list: **Red Hat Enterprise Linux ES release 4** and **Red Hat Enterprise Linux Server release 5**. Cheers – oHo Feb 02 '12 at 18:53
  • @Downvoter: Why? What did I miss? As far as I can tell, the accepted answer is doing the same lookup! – johnsyweb Feb 02 '12 at 20:57
6

The simpliest way is to use ps and grep:

command="httpd"
running=`ps ax | grep -v grep | grep $command | wc -l`
if [ running -gt 0 ]; then
    echo "Command is running"
else
    echo "Command is not running"
fi

If your command has some command arguments, then you can also put more 'grep cmd_arg1' after 'grep $command' to filter out other possible processes that you are not interested in.

Example: show me if any java process with supplied argument:

-Djava.util.logging.config.file=logging.properties

is running

ps ax | grep -v grep | grep java | grep java.util.logging.config.file=logging.properties | wc -l
Pawel Solarski
  • 1,028
  • 8
  • 7
6

Putting the various suggestions together, the cleanest version I was able to come up with (without unreliable grep which triggers parts of words) is:

kill -0 $(pidof mysql) 2> /dev/null || echo "Mysql ain't runnin' message/actions"

kill -0 doesn't kill the process but checks if it exists and then returns true, if you don't have pidof on your system, store the pid when you launch the process:

$ mysql &
$ echo $! > pid_stored

then in the script:

kill -0 $(cat pid_stored) 2> /dev/null || echo "Mysql ain't runnin' message/actions"
gletscher
  • 1,132
  • 2
  • 11
  • 18
5

Just a minor addition: if you add the -c flag to ps, you don't need to remove the line containing the grep process with grep -v afterwards. I.e.

ps acux | grep cron

is all the typing you'll need on a bsd-ish system (this includes MacOSX) You can leave the -u away if you need less information.

On a system where the genetics of the native ps command point back to SysV, you'd use

ps -e |grep cron

or

ps -el |grep cron 

for a listing containing more than just pid and process name. Of course you could select the specific fields to print out using the -o <field,field,...> option.

Tatjana Heuser
  • 964
  • 9
  • 11
  • How is this answer portable? (You say that different forms of the ps command should be used on different platforms) – peterh Oct 03 '13 at 10:55
  • ps unfortunately is one of those tools with a different set of options for the same result depending on their ancestry. So, unless you write your own (again incompatible to anything else) wrapper around this, the way to go would be to know the main lines of heritage and adapt accordingly. It is different when you're scripting - there you would use these differences to determine which branch you're on and adapt the behaviour of your script. Bottom line: you'll need to know both. Famous example: Larry Wall's "configure" script. Famous quote: Congratulations, you aren't running Eunice. – Tatjana Heuser Oct 04 '13 at 08:50
3

I use pgrep -l httpd but not sure it is present on any platform...
Who can confirm on OSX?

oHo
  • 51,447
  • 27
  • 165
  • 200
  • Thanks @Johnsyweb. Can you also check `pidof` please? OK you did. Thank you. So we should find something else working on OSX... Your basic `ps|grep` may be the single solution ;-) – oHo Feb 02 '12 at 18:15
1

This approach can be used in case commands 'ps', 'pidof' and rest are not available. I personally use procfs very frequently in my tools/scripts/programs.

   egrep -m1  "mysqld$|httpd$" /proc/[0-9]*/status | cut -d'/' -f3

Little explanation what is going on:

  1. -m1 - stop process on first match
  2. "mysqld$|httpd$" - grep will match lines which ended on mysqld OR httpd
  3. /proc/[0-9]* - bash will match line which started with any number
  4. cut - just split the output by delimiter '/' and extract field 3
Tom Lime
  • 1,154
  • 11
  • 15
1

You should know the PID of your process.

When you launch it, its PID will be recorded in the $! variable. Save this PID into a file.

Then you will need to check if this PID corresponds to a running process. Here's a complete skeleton script:

FILE="/tmp/myapp.pid"

if [ -f $FILE ];
then
   PID=$(cat $FILE)
else
   PID=1
fi

ps -o pid= -p $PID
if [ $? -eq 0 ]; then
  echo "Process already running."  
else
  echo "Starting process."
  run_my_app &
  echo $! > $FILE
fi

Based on the answer of peterh. The trick for knowing if a given PID is running is in the ps -o pid= -p $PID instruction.

Community
  • 1
  • 1
icarito
  • 328
  • 2
  • 11
1

None of the answers worked for me, so heres mine:

process="$(pidof YOURPROCESSHERE|tr -d '\n')"
if [[ -z "${process// }" ]]; then
  echo "Process is not running."
else
  echo "Process is running."
fi

Explanation:

|tr -d '\n'

This removes the carriage return created by the terminal. The rest can be explained by this post.

Jeff Luyet
  • 424
  • 3
  • 10
0

This prints the number of processes whose basename is "chromium-browser":

ps -e -o args= | awk 'BEGIN{c=0}{
 if(!match($1,/^\[.*\]$/)){sub(".*/","",$1)} # Do not strip process names enclosed by square brackets.
 if($1==cmd){c++}
}END{print c}' cmd="chromium-browser"

If this prints "0", the process is not running. The command assumes process path does not contain breaking space. I have not tested this with suspended processes or zombie processes.

Tested using gwak as the awk alternative in Linux.

Here is a more versatile solution with some example usage:

#!/bin/sh
isProcessRunning() {
if [ "${1-}" = "-q" ]; then
 local quiet=1;
 shift
else
 local quiet=0;
fi
ps -e -o pid,args= | awk 'BEGIN{status=1}{
 name=$2
 if(name !~ /^\[.*\]$/){sub(".*/","",name)} # strip dirname, if process name is not enclosed by square brackets.
 if(name==cmd){status=0; if(q){exit}else{print $0}}
}END{exit status}' cmd="$1" q=$quiet
}

process='chromium-browser'

printf "Process \"${process}\" is "
if isProcessRunning -q "$process" 
 then printf "running.\n"
 else printf "not running.\n"; fi

printf "Listing of matching processes (PID and process name with command line arguments):\n"
isProcessRunning "$process"
jarno
  • 787
  • 10
  • 21
0

Here is my version. Features:

  • checks for exact program name (first argument of the function). search for "mysql" will not match running "mysqld"
  • searches program arguments (second argument of the function)

script:

#!/bin/bash

# $1 - cmd
# $2 - args
# return: 0 - no error, running; 1 - error, not running
function isRunning() {
    for i in $(pidof $1); do
        cat /proc/$i/cmdline | tr '\000' ' ' | grep -F -e "$2" 1>&2> /dev/null
        if [ $? -eq 0 ]; then
            return 0
        fi
    done
    return 1
}

isRunning java "-Djava.util.logging.config.file=logging.properties"
if [ $? -ne 0 ]; then
    echo "not running, starting..."
fi
Raigedas
  • 346
  • 1
  • 6
0

[ $pid ] && [ -d /proc/$pid ] && command if you know the pid

André Willik Valenti
  • 1,702
  • 14
  • 21
-1

The following shell function, being only based on POSIX standard commands and options should work on most (if not any) Unix and linux system. :

isPidRunning() {
  cmd=`
    PATH=\`getconf PATH\` export PATH
    ps -e -o pid= -o comm= |
      awk '$2 ~ "^.*/'"$1"'$" || $2 ~ "^'"$1"'$" {print $1,$2}'
  `
  [ -n "$cmd" ] &&
    printf "%s is running\n%s\n\n" "$1" "$cmd" ||
    printf "%s is not running\n\n" $1
  [ -n "$cmd" ]
}

$ isPidRunning httpd
httpd is running
586 /usr/apache/bin/httpd
588 /usr/apache/bin/httpd

$ isPidRunning ksh
ksh is running
5230 ksh

$ isPidRunning bash
bash is not running

Note that it will choke when passed the dubious "0]" command name and will also fail to identify processes having an embedded space in their names.

Note too that the most upvoted and accepted solution demands non portable ps options and gratuitously uses a shell that is, despite its popularity, not guaranteed to be present on every Unix/Linux machine (bash)

jlliagre
  • 29,783
  • 6
  • 61
  • 72
  • `$ isPidRunning 0]` prints e.g. "0] is running 3 [ksoftirqd/0] 8 [rcuop/0] 17 [rcuos/0] 26 [rcuob/0] 34 [migration/0] 35 [watchdog/0] " here. – jarno Jun 19 '15 at 15:07
  • What do you need that PATH thing for? – jarno Jun 20 '15 at 10:13
  • I developed the solution further [here](http://stackoverflow.com/a/30936998/4414935). – jarno Jun 20 '15 at 11:43
  • @jarno The PATH setting is a requirement for the script to be portable. Otherwise, it would fail at least on Solaris 10 and older and possibly other Unix implementations. – jlliagre Jun 20 '15 at 11:46
  • @jarno Using `args` was indeed a mistake. I updated my solution to use `comm` which avoid the issue you pointed. Thanks! – jlliagre Jun 20 '15 at 11:57
  • Using `comm` is problematic in Linux, at least: column is cut in 15 characters. Thus the script will not find e.g. `chromium-browser`. Using `--cols 200` option or defining `-o comm:200=` does not help. I use `ps` of procps package, version 3.3.9. – jarno Jun 20 '15 at 17:12
  • In Ubuntu Linux `getconf PATH` outputs "/bin:/usr/bin". I wonder, if it has any effect there. – jarno Jun 20 '15 at 17:15
  • @jarno The 15 characters truncation was the reason why I originally used `args` instead of `comm`. I revert to `args` then, ignoring the `0]` case which while being technically valid is probably out of the OP scope. I can't use `cmd`, the OP is asking for a portable solution (Linux, Unix, OS X) but `cmd` is not specified by POSIX. About `/bin:/usr/bin`, there shouldn't be any issue with Ubuntu, this setting is limited to the `ps` and `awk` commands which will be undoubtedly found there. Funny to see the accepted and most voted solution provided doesn't match the OP requirements ... – jlliagre Jun 20 '15 at 21:21
  • In linux, `args`, `cmd` and `command` are aliases. Even `comm` may contain blanks, so `$2` may not equal to whole `comm` in the script. I wonder, if you can easily say where command ends and arguments start, if you use one of the former aliases. – jarno Jun 21 '15 at 16:09
  • I wonder, if you could use form `PATH=$(getconf PATH) ps ...` instead of exporting PATH? – jarno Jun 21 '15 at 16:32
  • 1
    @jarno I might do it but will need to repeat this PATH setting for awk too. Note that I reverted to the old backtick syntax to be portable with pre-POSIX syntax bourne shells. – jlliagre Jun 22 '15 at 12:37