Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.
Share on:

How to auto-restart services when down?

up down
Invicti Web Application Security Scanner – the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

Shell Script to Restart the Services if Down/Crashed

It’s common for the process to crash/go down due to various reasons, which you can investigate and fix the issues but that may take a little time.

However, one thing you can do it immediately to reduce the downtime for better availability is to automate restart of the process if it’s down.

Let’s get this done through the freeway – shell scripts

You can use following shell scripts to run through crontab, which will check the services at every 15 minutes (you can adjust the interval time) and will start if found not running. Sounds cool?

In this article, I will give two examples of starting the services if down.

If you are using CentOS/RHEL 7+ then you may also check this article explaining using systemd to restart.

Auto-restart MySQL, PHP-FPM, Nginx if down

A few weeks back, I moved Geek Flare to DigitalOcean with EasyEngine and MariaDB crashed twice in one week.

mariadb-crashed

You see it crashed early morning and was down for more than 3 hours, which is not good. It’s my blog, so no $$$ impact but I still feel bad for it.

Ok, showtime now…

  • Create a file using vi editors at your desired location (in this demo I will put it under /opt/startifdown.sh)
  • Copy & paste below script to the file and save it
#!/bin/bash
#Scripts to start services if not running
ps -ef | grep nginx |grep -v grep > /dev/null
if [ $? != 0 ]
then
       /etc/init.d/nginx start > /dev/null
fi
ps -ef | grep php5-fpm |grep -v grep > /dev/null
if [ $? != 0 ]
then
       /etc/init.d/php5-fpm start > /dev/null
fi
ps -ef | grep mysql |grep -v grep > /dev/null
if [ $? != 0 ]
then
       /etc/init.d/mysql start > /dev/null 
fi
  • Change the file permission to be executable
chmod 755 startifdown.sh

Test it manually to ensure the script is executable.

You may stop the service and use the script to check if it’s starting up. Once you are satisfied, you can put this in cron to run every 15 minutes.

*/1 * * * * /opt/startifdown.sh

Refer my crontab article if you need some inspiration in changing the interval time.

Now, this little script will take care of starting the services if crashed and I won’t have those 3 hours of downtime.

Much better!

Auto-restart WebSphere DMGR, Nodeagent, JVM if down

Create a file with following scripts – I will name it startwasifdown.sh

#!/bin/bash
#Scripts to start services if not running
#Specify DMGR Path Here
DMGR=/opt/IBM/WebSphere/AppServer/profiles/Dmgr01
#Specify Profile Path Here
Profile=/opt/IBM/WebSphere/AppServer/profiles/AppSrv01
ps -ef | grep dmgr |grep -v grep > /dev/null
if [ $? != 0 ]
then
       $DMGR/bin/startManager.sh > /dev/null
fi
ps -ef | grep nodeagent |grep -v grep > /dev/null
if [ $? != 0 ]
then
       $Profile/bin/startNode.sh > /dev/null
fi
ps -ef | grep server1 |grep -v grep > /dev/null
if [ $? != 0 ]
then
       $Profile/bin/startServer.sh server1 > /dev/null
fi

Note: Change the path to suit your environment and add more lines for more than one JVM.

  • Change the file permission to be executable
chmod 755 startwasifdown.sh

Test it manually, and once you are happy with it, you can put this in cron to run it every 15 minutes or whatever suits you.

*/1 * * * * /opt/ startwasifdown.sh

This is just a guideline for you to automate the stuff.

Love automation? Learn about bash shell scripting.

Thanks to our Sponsors
More great readings on Linux
Power Your Business
Some of the tools and services to help your business grow.
  • Invicti uses the Proof-Based Scanning™ to automatically verify the identified vulnerabilities and generate actionable results within just hours.
    Try Invicti
  • Web scraping, residential proxy, proxy manager, web unlocker, search engine crawler, and all you need to collect web data.
    Try Brightdata
  • Semrush is an all-in-one digital marketing solution with more than 50 tools in SEO, social media, and content marketing.
    Try Semrush
  • Intruder is an online vulnerability scanner that finds cyber security weaknesses in your infrastructure, to avoid costly data breaches.
    Try Intruder