- Create bash script file, eg.
nano /opt/scripts/reload-service.sh
, add these commands in the file:
#!/bin/bash
NOW=$(date +"%d.%m.%Y_-_%H:%M:%S")
systemctl -q is-active mongod.service || \
echo "$NOW Mongod service down, restarting..." \
sudo service mongod restart
-
Make it runnable:
sudo chmod +x ./reload-service.sh
-
Schedule periodical cron checks via
sudo crontab -e
:
# periodically check if mongod service is running, restart it on crash
*/1 * * * * /opt/scripts/reload-service.sh >> /opt/scripts/reload-service.log 2>&1
Inspiration:
https://zeropointdevelopment.com/how-to-use-a-bash-script-to-restart-linux-server-services/