Skip to content

Instantly share code, notes, and snippets.

@jbutko
Last active June 7, 2021 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbutko/e30cbca9ffae734ae8d52ff0085daf11 to your computer and use it in GitHub Desktop.
Save jbutko/e30cbca9ffae734ae8d52ff0085daf11 to your computer and use it in GitHub Desktop.
Auto restart system service (mongod) on crash
  1. 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
  1. Make it runnable: sudo chmod +x ./reload-service.sh

  2. 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/

https://unix.stackexchange.com/a/500336

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment