watchdog How to restart a process out of crontab on a Linux/Unix

How to restart a process out of crontab on a Linux/Unix

last updated in Categories , , , , ,

 

I am a new Linux user and not familiar with crontab syntax. How do I use a cron job to restart server process if not running on a Linux or Unix-like system?

There are many ways to make sure that the essential server process such as HTTPD/Nginx/PHP-FPM/MySQL remains up and running all the time.
restarting a process out of crontab
This tutorial shows how to create and use a simple bash shell script to restart failed services using crontab.

 

 

 

Step 1 – Create a Bash Shell Script

Here is a simple shell script to check for httpd pid using the pgrep command. It will restart if httpd is not running on a CentOS/RHEL using the systemctl command. Create a file named /root/bin/restart-httpd
# vi /root/bin/restart-httpd
Append the following code:

#!/bin/bash
# Apache Process Monitor
# Restart Apache Web Server When It Goes Down
#  RHEL / CentOS 7.x httpd restart command
RESTART="/bin/systemctl restart httpd"
 
#Path to pgrep command
PGREP="/usr/bin/pgrep"
 
# Httpd daemon name: Under RHEL/CentOS/Fedora it is httpd
HTTPD="httpd"
 
# Find httpd pid
$PGREP ${HTTPD}
 
if [ $? -ne 0 ] # if apache not running 
then
 # restart apache
 $RESTART
fi

Save and close the file. Set permissions:
# chmod +x /root/bin/restart-httpd
# /root/bin/restart-httpd

Step 2 – Setup the cron job to restart process

Cron allows Linux and Unix users to run commands or scripts at a given date and time. You can schedule scripts to be executed periodically. Type the following command:
# crontab -e
Append the following code to restart httpd
## restart httpd if not running. check for httpd every 5 mins
*/5 * * * * /root/bin/restart-httpd >/dev/null 2>&1

Save and close the file.

A note about Linux and systemd based system

Systemd can handle keeping the service alive for you. You need to use the cp command:
# cp /lib/systemd/system/httpd.service /etc/systemd/system/httpd.service
Edit the file using vi command:
# vi /etc/systemd/system/httpd.service
Add Restart=always and RestartSec=5 in the [Service] section as follows:

[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
 
[Service]
##############################################
## Make sure httpd always restart if failed ##
## Configures the time to sleep before      ##    
## restarting a service with 5 seconds too  ##
##############################################
Restart=always
RestartSec=5
##############################################
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

Reload configuration:
# systemctl daemon-reload
# systemctl restart httpd

Test it by killing httpd using the kill command:
# kill -9 $(pgrep httpd)
After 5 seconds httpd will restart again. Verify with the ps command:
# ps aux | grep httpd
Sample outputs:

root      8052  0.0  0.0 221960  7580 ?        Ss   13:46   0:00 /usr/sbin/httpd -DFOREGROUND
apache    8053  0.0  0.0 221960  6096 ?        S    13:46   0:00 /usr/sbin/httpd -DFOREGROUND
apache    8054  0.0  0.0 221960  6096 ?        S    13:46   0:00 /usr/sbin/httpd -DFOREGROUND
apache    8055  0.0  0.0 221960  6096 ?        S    13:46   0:00 /usr/sbin/httpd -DFOREGROUND
apache    8056  0.0  0.0 221960  6096 ?        S    13:46   0:00 /usr/sbin/httpd -DFOREGROUND
apache    8057  0.0  0.0 221960  6096 ?        S    13:46   0:00 /usr/sbin/httpd -DFOREGROUND
root      8061  0.0  0.0 112644  1720 ?        S+   13:46   0:00 grep --color=auto httpd

Conclusion

This is easy to setup and uses method to make sure an app/service remain online if killed due to some issues. A better and more reliable solution is monit monitoring software for restarting services such as mysql, apache and sendmail under a UNIX / Linux operating systems:

Monit is a free and open source software that acts as process supervision. It comes with the ability to restart services which have failed. You can use Systemd, daemontools or any other such tool for the same purpose.

0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
LVM: Extend an existing Volume Group by adding a new disk
Viewed 1637 times since Sat, Jun 2, 2018
bash mistakes This page is a compilation of common mistakes made by bash users. Each example is flawed in some way.
Viewed 8591 times since Sun, Dec 6, 2020
Turbocharge PuTTY with 12 Powerful Add-Ons – Software for Geeks #3
Viewed 14052 times since Sun, Sep 30, 2018
LVM: Extend an existing Logical Volume / Filesystem
Viewed 2061 times since Sat, Jun 2, 2018
Get UUID of Hard Disks [Update]
Viewed 1808 times since Tue, Jul 17, 2018
Managing temporary files with systemd-tmpfiles on Red Hat Enterprise Linux 7
Viewed 8765 times since Sun, Nov 22, 2020
LVM: Rename root VG/LV
Viewed 6959 times since Sat, Jun 2, 2018
socat: Linux / UNIX TCP Port Forwarder
Viewed 8640 times since Tue, Aug 6, 2019
How to remove CTRL-M (^M) characters from a file in Linux
Viewed 1938 times since Thu, Feb 7, 2019
red hat 7 tmpfiles service
Viewed 1409 times since Thu, Oct 11, 2018