Categories
Uncategorized

Restart service if website is down with cronjob

This can be easily achieved on a linux server with the help of a cronjob.

Open up your terminal and edit your cronjobs list using sudo crontab -e.

# Run once every minute
* * * * * /usr/bin/wget "www.example.com/testfile.php" --timeout 30 -O - 2>/dev/null  | grep "Success text here" ||  /etc/init.d/mysql start
* * * * * service mysql status || service mysql start

Save with Ctrl+X -> Y -> Enter.

Multiple Services

You can also chain multiple service commands using ‘&&’ to execute if the prior command has completed successfully.

* * * * * /usr/bin/wget "www.example.com/testfile.html" --timeout 30 -O - 2>/dev/null  | grep "success string" ||  /etc/init.d/apache2 start &&  /etc/init.d/hhvm start

 

Send Notification Email

You can also set it to send you an email if you want to be notified of the issue.

 * * * * * /usr/bin/wget "www.example.com/testfile.php" --timeout 30 -O - 2>/dev/null  | grep "Success text here" ||  /etc/init.d/mysql start | /usr/bin/mail -v -s "Error message here" email@domain.com