160

How can I determine if a process is running or not and then have a bash script execute some stuff based on that condition?

For example:

  • if process abc is running, do this

  • if it is not running, do that.

the
  • 805
  • 1
  • 9
  • 16
Nirmik
  • 7,868
  • 2
    Feel it important to note that none of the solutions below take into account the state of the process. A comment on one of my questions brought me here, but an answer on it clued me into different state's of a program, like zombie process' (Not what I would describe as a "running" process). A full list of what the STAT column values, in the output of ps, indicates is here for those inclined to write an answer that accommodates this, or edit their own. – user66001 Oct 19 '13 at 17:54
  • 1
    Related discussion: http://unix.stackexchange.com/questions/74185/how-can-i-prevent-grep-from-showing-up-in-ps-results – blong Jun 17 '14 at 17:08
  • 1
    best way to check for process exists: http://stackoverflow.com/questions/3043978/how-to-check-if-a-process-id-pid-exists – Trevor Boyd Smith Jun 07 '16 at 15:44
  • 3
    By pid: https://stackoverflow.com/questions/3043978/how-to-check-if-a-process-id-pid-exists – Alberto Salvia Novella Feb 19 '21 at 06:50
  • 2
    By path: https://stackoverflow.com/questions/29260576/check-if-a-process-is-running-using-bash – Alberto Salvia Novella Feb 19 '21 at 06:50

12 Answers12

241

A bash script to do something like that would look something like this:

#!/bin/bash

# Check if gedit is running
# -x flag only match processes whose name (or command line if -f is
# specified) exactly match the pattern. 

if pgrep -x "gedit" > /dev/null
then
    echo "Running"
else
    echo "Stopped"
fi

This script is just checking to see if the program "gedit" is running.

Or you can only check if the program is not running like this:

if ! pgrep -x "gedit" > /dev/null
then
    echo "Stopped"
fi
John Vrbanac
  • 4,627
  • @DreadPirateShawn can you tell me why use /dev/null and not simply a 0? Using the number makes the code way more readable, at least for a noob (such as me). No redirect, no nothing – Silviu Nov 09 '15 at 18:46
  • 4
    @Silviu huh? Googling /dev/null: "/dev/null redirects the command standard output to the null device, which is a special device which discards the information written to it" ... using 0 would redirect the command output to a file called 0. In this case, I'd advise becoming more comfortable with > /dev/null -- you'll see it everywhere, as it's the standard / proper way to discard output. – DreadPirateShawn Nov 10 '15 at 05:33
  • Woot thanks, I used this for a nice little script that checks if a program is running before it executes. (although I had to extend it a little since google chrome's executable doesn't have the same name as it's run command, the exec name is just chrome.) – Cestarian Jan 31 '16 at 18:22
  • 4
    Please add the -x parameter to pgrep so that it is looking for the exact application or else it will get a false correct if it finds the same string inside the name of another app. I just spent 1 hour to find that out. – Thanos Apostolou Aug 31 '16 at 21:24
  • 1
    To check if the program is not running use ! pgrep – Mohammed Noureldin Jan 21 '17 at 22:29
  • Append to @Thanos' comment, in case the process name is longer than 15 characters, one should use -f so pgrep can find the exact name. For example, if 111112222233333xx process is running and its PID is 1234, then pgrep -x 111112222233333xx; echo $? return 1. pgrep -x 111112222233333; echo $? return 1234 0. pgrep -f 111112222233333xx; echo $? return 1234 0. – Lam Nov 02 '21 at 08:35
42

Any solution that uses something like ps aux | grep abc or pgrep abc are flawed.

Why?

Because you are not checking if a specific process is running, you are checking if there are any processes running that happens to match abc. Any user can easily create and run an executable named abc (or that contains abc somewhere in its name or arguments), causing a false positive for your test. There are various options you can apply to ps, grep and pgrep to narrow the search, but you still won't get a reliable test.

So how do I reliably test for a certain running process?

That depends on what you need the test for.

I want to ensure that service abc is running, and if not, start it

This is what systemd is for. It can start the service automatically and keep track of it, and it can react when it dies.

See How can I check to see if my game server is still running... for other solutions.

abc is my script. I need to make sure only one instance of my script is running.

In this case, use a lockfile or a lockdir. E.g.

#!/usr/bin/env bash

if ! mkdir /tmp/abc.lock; then
    printf "Failed to acquire lock.\n" >&2
    exit 1
fi
trap 'rm -rf /tmp/abc.lock' EXIT  # remove the lockdir on exit

# rest of script ...

See Bash FAQ 45 for other ways of locking.

geirha
  • 46,101
  • 7
    While this is technically true, I've ne er personally encountered such a problem in real life. Most programs don't change their names in ways that break scripts. Thus, for simple scripts something like pgrep or ps is perfectly adequate and your approach seems like overkill. If you're writing a scrip for public distribution, though, you should write it in the safest way possible. – Scott Severance Jun 30 '12 at 13:21
  • 2
    @ScottSeverance Programs changing names is not the issue; that would require intervention regardless. It's other users running the same program, or other programs with similar names that will suddenly cause the script to get false positives and thus do the wrong thing. I just prefer "works" rather than "mostly works". – geirha Jun 30 '12 at 14:15
  • 3
    I misspoke. But many of us run single-user systems. And in a multiple user situation, it's easy to also grep for the username. – Scott Severance Jun 30 '12 at 21:03
  • 1
    what if your script fails during runtime and dies prior to unlocking the file? – jp093121 Aug 14 '14 at 17:41
  • 2
    @jp093121 The EXIT trap is triggered when the script exits. Whether it exits because it reaches end of script, an exit command, or receives a signal (that can be handled), the rm command is run. So as long as it ends after the trap has been set, the lock should be gone. – geirha Aug 16 '14 at 18:30
  • I think rm is not run when script exits due to reaching its end. I don't know why, but in my case I had to manually add a rm -rf as the last line. – Kazim Zaidi Jun 21 '16 at 06:46
  • @KazimZaidi various reasons why that could happen. A typo could cause it, expanding a variable at the wrong time, or the trap being set in a subshell. Those are just three things I can think of right now. Without more context, it's not possible to say why the cleanup doesn't happen in your case. – geirha Jun 21 '16 at 17:49
  • Anything is flawed if you do it in a flawed matter. One should be setting up a proper regex and using -f option with pgrep to avoid false positives. Also, you can't set up lock files on process which is a compiled executable and/or doesn't come with its source code. – Sergiy Kolodyazhnyy Jan 28 '17 at 18:44
  • Typo: acquire not aquire – Adam Plocher Jan 04 '18 at 00:31
  • 1
    Unanticipated power failure leaves the lock file behind, so running it automatically at the next boot will take a separate loader boot script that first deletes the lock file then executes scriptfile & – SDsolar May 05 '18 at 02:16
  • Ah, the inevitable "What you want to do is considered harmful [by me]!" non-answer that wastes everyone's time without actually answering the question to any meaningful degree. Negligible bonus points for both name-dropping an obsolete init daemon (upstart) and failing to name-drop the most widely adopted init daemon (systemd). Well done, @geirha. Well done. – Cecil Curry Jan 30 '20 at 02:43
  • I'd like to offer something in between basically this would work and will not give false positive with grep showing up in ps: ps -aux | grep "process" | grep -v "grep" | awk '/process/{print $2}' – YCN- Feb 10 '21 at 09:54
21

This is what I use:

#!/bin/bash

#check if abc is running
if pgrep abc >/dev/null 2>&1
  then
     # abc is running
  else
     # abc is not running
fi

In plain English: if 'pgrep' returns 0, the process is running, otherwise it is not.


Related reading:

Bash Scripting :: String Comparisons

Ubuntu Manuals pgrep

Zuul
  • 1,974
7

I usually have a pidof -x $(basename $0) on my scripts to check if it's already running.

Rommel Cid
  • 79
  • 1
  • 1
4

Riffing on @rommel-cid's idea, you can use pidof with the || (||) to run a command if the process does not exist and && to run something if the process does exist, thus creating a quick if/then/else conditional. For example here's one with a running process (my chrome browser, whose process name is "chrome") and one testing for a process that does not exist. I suppressed the standard output using 1>/dev/null so that it doesn't print:

$ (pidof chrome 1>/dev/null && echo "its running? ok, so am i then" ) || echo "it's not running? ok i'll run instea\
d"
its running? ok, so am i then
$ (pidof nosuchprocess 1>/dev/null && echo "its running? ok, so am i then" ) || echo "it's not running? ok i'll run\
 instead"
it's not running? ok i'll run instead
$
yuvilio
  • 3,389
3

By pid:

test -d /proc/[pid]

By name:

pgrep -u [user] -x [name] >/dev/null

"-x" means "exact match".

3
## bash

## function to check if a process is alive and running:

_isRunning() {
    ps -o comm= -C "$1" 2>/dev/null | grep -x "$1" >/dev/null 2>&1
}

## example 1: checking if "gedit" is running

if _isRunning gedit; then
    echo "gedit is running"
else
    echo "gedit is not running"
fi

## example 2: start lxpanel if it is not there

if ! _isRunning lxpanel; then
    lxpanel &
fi

## or

_isRunning lxpanel || (lxpanel &)

Note: pgrep -x lxpanel or pidof lxpanel still reports that lxpanel is running even when it is defunct (zombie); so to get alive-and-running process, we need to use ps and grep

Bach Lien
  • 139
  • 4
  • 1
    Substantially superior to any other answers listed here, despite receiving not a single upvote. So, why is this the ideal specimen? Let us enumerate the reasons why: (A) it's shell-agnostic (despite specifying bash), (B) it's distro-agnostic (because it only leverages ps and grep), (C) it's resilient against false positives (because it passes -o and -C to ps and -x to grep), and (D) it's resilient against zombie processes (as noted). In short, it's the CLI equivalent of spring-fed glacier water imported at considerable expense from the fjords of Norway. – Cecil Curry Jan 30 '20 at 02:53
1

None of the "simple" solutions worked for me because the binary I need to check is not installed system-wide, so I have to check with path, which in turn requires using ps -ef | grep approach:

app="$_sdir/Logic 1.2.18 (64-bit)/Logic"

app_pid=`ps -ef | grep "$app" | awk '{print $2}'`

if `ps -p $app_pid > /dev/null`; then
    echo "An instance of logic analyzer is appear to be running."
    echo "Not starting another instance."
    exit 5
else
    nohup "$app" &> /dev/null &
fi
ceremcem
  • 179
  • 5
0

First thing that came to my mind for your problem:
ps aux | grep -i abc will show the details of the process if its running. You may match the number of lines or time for which its running and compare with zero or any other manipulation. When you run the above command it will show you atleast one line of output i.e. detail about the process created by thi grep command.. So take care of that.
That should do as a simple hack. Put it in the bash script and see if its helpful.

drake01
  • 3,457
0

Using start-stop-daemon:

/sbin/start-stop-daemon --background --make-pidfile --pidfile /tmp/foo.pid -S --startas /usr/bin/program -- arg1 arg2

It works as normal user.

-1

I found that the accepted answer posted by @John Vrbanac did not work for me, and that the answer posted by @geirha doesn't answer the original question.

John Vrbanac's solution didn't work to check if a PHP process was running or not for me, I'm running CentOS 7.

@geirha's answer only makes sure an instance isn't already running before starting another. This was not the original question, the original question was to check if a process is running or not.

Here's what worked for me:

Say my process had the string "Jane" in it's process name. This will find if it's running or not. This works for BASH and PHP scripts.

ps -aux | grep "[J]ane" > /dev/null 2>&1
if [[ "$?" == "0" ]]; then
    echo "It's running"
else
    echo "It's not running"
fi
-1
#!/bin/bash
while [ true ]; do     # Endless loop.
  pid=`pgrep -x ${1}`  # Get a pid.
  if [ -z $pid ]; then # If there is none,
    ${1} &             # Start Param to background.
  else
    sleep 60           # Else wait.
  fi
done