1

I'm using centOS 7 and I'm trying to make together these walktroughs:

Script to check if process is running

Github's example of making sidekiq run as service

Nevertheless, both look very smart, I've got stuck when I tried to check the script from the first one manually.

So in /etc/cron.hourly I placed sidekiq_restart with the following script:

    #!/bin/bash
# A simple script to check if a process is running and if not will
# restart the process and send a mail.
################################################
# The name of the program we want to check
PROGRAM=sidekiq

# The user we would like notified of the restart
MAILUSER="someone@weeenospam.blah"
################################################

PROCESSPID=$(pidof -s $PROGRAM)
if [ -z "$PROCESSPID" ];
then
# Use systemctl
systemctl stop $PROGRAM.service
systemctl start $PROGRAM.service
# Comment above and uncomment below to use service rather than systemctl
# service $PROGRAM restart
echo mail -s "Service $PROGRAM was found to be stopped on $HOSTNAME at $(date) and has been restarted" $MAILUSER << /dev/null
echo "$PROGRAM had FAILED on $HOSTNAME @ $(date)" >> $PROGRAM-check.log
else
echo "$PROGRAM was running ok on $HOSTNAME @ $(date)" >> $PROGRAM-check.log
fi
exit

I ran sidekiq as service with:

systemctl start sidekiq

And when I check with ps -aux | grep [s]idekiq:

deploy_+  9883 36.4  0.6 474972 100292 ?       Ssl  14:23   0:02 sidekiq 5.1.3 pnvstart [0 of 20 busy]

Looks perfect! But when I try:

pidof -s sidekiq

It returns just nothing! Of course that means script will be wrong! How to fix that? Thanks in advance!

  • I'm confused - the service file you link to will already restart sidekiq if it crashes (Restart=on-failure). – Ulrich Schwarz May 4 '18 at 12:36
0

From your ps output, it looks like sidekiq changes its own process name to include runtime information: sidekiq 5.1.3 pnvstart [0 of 20 busy]. In that case, pidof probably doesn't find it because it's looking for exactly "sidekiq".

If you don't plan on starting and stopping sidekiq manually, you can use systemd's own tools: systemctl is-active sidekiq will return with error code if sidekiq is not running, and succesfully if it is.

Personally, I'm a friend of exit-soon, so I'd write code along the lines of

systemctl is-active sidekiq && exit # all is well

# oh no, it's gone!
systemctl restart sidekiq
mail -s ...
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.