Check if a process is running and if not, restart it using Cron
To exclude the grep
result from the ps
output. Do
ps -aux | grep -v grep | grep sidekiq
(or) do a regEx
search of the process name, i.e. s
followed by rest of the process name.
ps -aux | grep [s]idekiq
To avoid such conflicts in the search use process grep
pgrep
directly with the process name
pgrep sidekiq
An efficient way to use pgrep
would be something like below.
if pgrep sidekiq >/dev/null
then
echo "Process is running."
else
echo "Process is not running."
fi
iCyborg
Updated on June 15, 2022Comments
-
iCyborg almost 2 years
I have created restart.sh with followin code
#!/bin/bash ps -aux | grep sidekiq > /dev/null if [ $? -eq 0 ]; then echo "Process is running." else echo "Process is not running." fi
To check if sidekiq process is running or not. I will put this script in cron to run daily so if sidekiq is not running, it will start automatically.
My problem is, with
ps -aux | grep sidekiq
even when the process is not running, it shows
myname 27906 0.0 0.0 10432 668 pts/0 S+ 22:48 0:00 grep --color=auto sidekiq
instead of nothing. This gets counted in grep hence even when the process is not running, it shows as "sidekiq" process is running. How to not count this result ? I believe I have to use awk but I am not sure how to use it here for better filtering.
-
sjsam over 7 yearsHmm. Nice touch :) I guess the op has already figured out the root cause of the problem which is the line
ps -aux | grep sidekiq > /dev/null
in his script..In particular I liked the second solution which is clinical. -
iCyborg over 7 years@inian - grep -v grep worked perfectly and now the script is running. pgrep sidekiq somehow is giving nothing when I run in terminal even though the process is running. May be it is because the way sidekiq works ?
-
ghoti over 7 yearsThere's no need to redirect things to
/dev/null
. Just use the-q
option, for eithergrep
orpgrep
. -
Ian Gibbs almost 5 yearsNot all versions of pgrep support
-q