Best way to send email when PHP process dies Best way to send email when PHP process dies nginx nginx

Best way to send email when PHP process dies


What about use a cronjob (bash based) to parse error_log file periodically (x hours) and send an email (mutt/mail) when find something like resuming normal operations in the last period (x hours). I think is simple and effective...

[Thu Dec 27 14:37:52 2012] [notice] caught SIGTERM, shutting down[Thu Dec 27 14:37:53 2012] [notice] Apache/2.2.22 (Ubuntu) PHP/5.4.6-2~precise+1 configured -- resuming normal operations

UPDATE:

@Brian As @takeshin says cronjobs can run even every second if you want, but some sysadmins could bite you... :|


Here is what I've ended up doing. I've not rolled it out to our prod servers yet, but all testing thus far looks good.

Nginx does not support CGI natively, so you need another means to do it. thttpd fit the bill nicely. There is a good write up the nginx wiki showing how to use it.

I configured thttpd with the following:

dir=/var/www/htdocsuser=thttpdlogfile=/var/log/thttpd.logpidfile=/var/run/thttpd.pidport=8000cgipat=**.cgi

And added this to my nginx config:

error_page 502 @thttpd;location @thttpd {    include proxy.include;    proxy_pass http://127.0.0.1:8000;}

Finally, I created a basic CGI script that calls PHP on the command line and passed in my already-written PHP script. This was an ideal solution for me because the script was already set up to log to our alerts table and fire off an email. This is also real-time, as the script will execute as soon as nginx returns a 502 code (subsequent 502s will not hammer me with emails, per the logic of the script).

I was able to run some simulation tests be forcing nginx to return a 502 (see more here).

I'm going to continue tweaking this, but I'm pretty happy with the relative ease of deploying it and that I could re-use existing code.


We have dual solution.

We use shell script to send out email notifications, if PHP dies. We check if php service is running with shell command in the shell script, if it is not running, we'll fire off a shell command to send an email.

This is all in a few lines of Shell Script. Not too hard.

Of course, set it up in cron.