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 thisif it is not running, do that.
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.
A bash script to do something like that would look something like this:
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:
Any solution that uses something like
ps aux | grep abc
orpgrep 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 namedabc
(or that containsabc
somewhere in its name or arguments), causing a false positive for your test. There are various options you can apply tops
,grep
andpgrep
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.
See Bash FAQ 45 for other ways of locking.
This is what I use:
In plain English: if 'pgrep' returns 0, the process is running, otherwise it is not.
Related reading:
Bash Scripting :: String Comparisons
Ubuntu Manuals pgrep
I usually have a
pidof -x $(basename $0)
on my scripts to check if it's already running.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:Note:
pgrep -x lxpanel
orpidof lxpanel
still reports thatlxpanel
is running even when it is defunct (zombie); so to get alive-and-running process, we need to useps
andgrep
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: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.
Using
start-stop-daemon
:It works as normal user.
By pid:
By name:
"-x" means "exact match".