Start services on CentOS

bash icon

I had a lot of issue with php-fpm and nginx services being run on this very server, they goes in to dead state with the error: service dead but pid file exists. And sometimes it happens that some service gets exited without any valid error or anything, and if you are running a small blog like me, then you do not goto the extent to have another server and configure nagios which will keep track of your server services and files. Hence, the idea of a script came to my mind. What this simple script does is, check the status of three services (which are mentioned in the script and you may add more), if the serviceĀ  is running, no action is taken, if the service is not running for any reason, this script will try to start that particular service. Moreover, if there are issues while starting a particular service, the script will shoot an email to the user (also mentioned in the script) with the status.

After that, I added a cron to run this script every 5 minutes. So basically these three services (php-fpm, mysql and nginx) are checked every 5 minutes. This serves the purpose until you find the root cause of the error mentioned above. Feel free to use this script at your discretion and make changes to it.


#!/usr/bin/env bash
#
# Date: 20 November, 2014
# Author: Aman Hanjrah
# License: GPL
#
# About: This is a minimal script that will check the status of services (php-fpm, mysql and nginx), if some problem is found, it will try to restart the service automatically, if not successful, it will shoot an email as defined in "$EMAIL" variable
#
# Start
# Define administrator email address
EMAIL="email.address@domain.com"
#
# Check php-fpm service
function chkPhpFpm() {
CHK_FPM=$(/etc/init.d/php-fpm status)
if [[ $(echo $?) != 0 ]]; then
strtPhpFpm
fi
}

# Try to restart the php-fpm service if issues are encountered
function strtPhpFpm() {
STRT_FPM=$(/etc/init.d/php-fpm restart)
if [[ $(echo $?) != 0 ]]; then
# Shoot an email to administartor in case restart fails
echo -e "An attempt to restart php-fpm service FAILED!" | mutt -s "MANUAL INTERVENTION REQUIRED - php-fpm" -- $EMAIL
else
chkPhpFpm
fi
}

# Check nginx service
function chkNginx() {
CHK_NGINX=$(/etc/init.d/nginx status)
if [[ $(echo $?) != 0 ]]; then
strtNginx
fi
}

# Try to restart the php-fpm service if issues are encountered
function strtNginx() {
STRT_NGINX=$(/etc/init.d/nginx restart)
if [[ $(echo $?) != 0 ]]; then
# Shoot an email to administartor in case restart fails
echo -e "An attempt to restart nginx service FAILED!" | mutt -s "MANUAL INTERVENTION REQUIRED - nginx" -- $EMAIL
else
chkNginx
fi
}

# Check MySQL service
function chkMysql() {
CHK_MYSQL=$(/etc/init.d/mysqld status)
if [[ $(echo $?) != 0 ]]; then
strtMysql
fi
}

# Try to restart the php-fpm service if issues are encountered
function strtMysql() {
STRT_NGINX=$(/etc/init.d/mysqld restart)
if [[ $(echo $?) != 0 ]]; then
# Shoot an email to administartor in case restart fails
echo -e "An attempt to restart MySQL service FAILED!" | mutt -s "MANUAL INTERVENTION REQUIRED - mysql" -- $EMAIL
else
chkMysql
fi
}

function main() {
chkPhpFpm
chkNginx
chkMysql
}
main
# End

After that, just add a cron entry as following:


# crontab -e

*/5 * * * * /bin/sh /path/to/script_name.sh > /dev/null

In case you have any comments or questions, or advice on how can I make this script more advanced, feel free to update the comments section below.

Leave a Reply

Your email address will not be published. Required fields are marked *