Recently, my site's visitor-ship increased sharply. My Apaches' always crash. I'm seeing lines like these in the httpd/error_log:
[Sun Apr 13 08:57:18 2014] [warn] child process 7792 still did not exit, sending a SIGTERM
[Sun Apr 13 08:57:20 2014] [error] child process 7925 still did not exit, sending a SIGKILL
...
I will be upgrading my server in the next month. Before that, I'm trying to write a Bash script to automatically restart Apache with a crond job, which checks every 5 minutes.
If curling some page does not return a 200 code, then fuser -k -n tcp 80 and /etc/init.d/httpd restart.
I'm not good at Bash scripting. I've found some working code which will only restart Apache.
#!/bin/bash
curl -I http://www.mydomain.com/some-empty-page 2>/dev/null \
| head -1 | grep -q " 200 OK"
if [ $? -eq 0 ]; then
echo ""
else
/etc/init.d/httpd restart
echo "wrong $(date)" >> /home/myspace/restart_log.txt
fi
How can I modify this to kill all the jobs which use port 80 before restarting?
I'm intending to insert some working line like:
else fuser -k -n tcp 80
/etc/init.d/httpd restart
echo "wrong $(date)" >> /home/myspace/restart_log.txt
fi
NOTE: first else fuser -k -n tcp 80, then do /etc/init.d/httpd restart in the else case.
/etc/init.d/httpd restartwill restart Apache, you don't need to kill anything else beforehand.else fuser -k -n tcp 80; /etc/init.d/httpd restart;the script can not run. I monitor the/home/myspace/restart_log.txt./home/myspace/restart_log.txt,if nofuser -k -n tcp 80;, if the apache crash down, it will insert new line aboutwrong $(date), but if addfuser -k -n tcp 80;no new line insert intorestart_log.txtand apache remains in a crash status.