Linux – Running a bash script via cronjob to restart a service

bashcronlinux

First of all. I don't know anything about bash scripting. second this is a very big problem on my side, and I don't have any idea what to do.

Currently, I have this website. which was written in php. and uses mail server and push notification which are all runned as daemon using supervisord. My site is running well but from time to time, my mail server and push notification doens't work because the supervisord stopped working; I had to restart it manually to make it working again.

I was thinking, what could I do to have it auto start.
then an idea popped up. to restart the service via bash script and cron jobs. I know it was kinda dumb.

I created a bash script . just a simple one.

#!/bin/bash
# A simple script 
echo " -- Restarting Supervisord --"
sudo service supervisor stop
sudo service supervisor start
echo "Done!" 

when I run this manually via

./supervisord_restart.sh 

this was the output I've got

 -- Restarting Supervisord --
Job for supervisor.service failed. See "systemctl status supervisor.service" and "journalctl -xe" for details.
Done!

allso I don't konw if this is right in cronjob

0   0,12    *   *   *   /supervisord_restart.sh

by the way if I am doing right. I need my bash script to check if suprevisord is running then execute the commands.

Best Answer

First of all: you should investigate the "real" problem with supervisord instead of restarting the service every night.

That being said, try to use /etc/init.d/supervisor stop / start or restart. Many times cron has problems using service. You also have to create the cron as the user supervisord runs on, or as root via sudo crontab -e. Also always use complete paths in cron, so not /supervisord_restart.sh but /usr/local/bin/supervisord_restart.sh or similar.

(And: what Linux distro are you running)