Shell Script - if service stopped , script start the service
-
sometimes My mongod service is stopping & I have to restart the MONGOD service to start the service . can I create a script "its check every 5 minutes or 10 miutes that service is in start mode or stop mode" & if it is in stopped condition then its start with that script.
Please guide
-
create a script:
sudo nano /opt/relaunch-stopped-services.sh
#!/bin/bash service YOUR_SERVICE_NAME_HERE status | grep 'active (running)' > /dev/null 2>&1 if [ $? != 0 ] then sudo service YOUR_SERVICE_NAME_HERE restart > /dev/null fi
Then,
make it executable:
sudo chmod +x /opt/relaunch-stopped-services.sh
Next,
You need to edit your root crontab list
sudo crontab -e
and not your user profile crontab.*/5 * * * * /opt/relaunch-stopped-services.sh > /dev/null 2>
This script will run every 5 minutes.
Full details: https://zeropointdevelopment.com/how-to-use-a-bash-script-to-restart-linux-server-services/
-
Thank you so much.