Linux: Cron job to monitor for php-fpm service problem


Sometimes, php-fpm service is error, how to monitor for php-fpm service ?

Step 1: create a file like check.php with a simple content like

<?php
echo "1";

and upoad this file to document root (like: /home/vpssim.demo/public_html), you will have the link: http://localhost/check.php

Step 2: add a cron job:

*   *   *   *   *    /usr/bin/curl --head -sf http://localhost/check.php -o /dev/null || /usr/sbin/service php-fpm restart

You can use the command line

crontab -e

Then type: i
Then type:

*   *   *   *   *    /usr/bin/curl --head -sf http://localhost/check.php -o /dev/null || /usr/sbin/service php-fpm restart

Escape
Then type: :w
Then type: :q

Done! now when php-fpm is error, this service will be restarted

To see list of cron jobs, you can use command

crontab -l

Method 2: (better than the first) you can use this command line:

[root@tutorialspots ~]# /usr/bin/wget --timeout 4 --tries 1 http://localhost/cron.php -O /dev/null || (/usr/sbin/service php-fpm stop && /usr/sbin/service php-fpm start)

Result:

[root@tutorialspots ~]# /usr/bin/wget --timeout 4 --tries 1 http://localhost/cron.php -O /dev/nu
ll || (/usr/sbin/service php-fpm stop && /usr/sbin/service php-fpm start)
--2018-01-30 10:48:42--  http://localhost/cron.php
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:80... failed: Connection refused.
Connecting to localhost (localhost)|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... Read error (Connection timed out) in hea
ders.
Giving up.

Redirecting to /bin/systemctl stop  php-fpm.service
Redirecting to /bin/systemctl start  php-fpm.service

Leave a Reply