问题
I have the following:
kill -9 `ps aux | grep php | awk '$9 !~ /[0-9]:[0-9]/' | awk '{print $2}'`
What it does is kill process that have been left abandoned by fcgid and kills them to free RAM. I want to run this as a cron every hour but would like to kill processes older then an hour. I'm just not sure how to modified the script to do that.
回答1:
Try the following bash code :
for i in $(pidof php); do
pidtime=$(stat -c '%Y' /proc/$i)
now=$(date +%s)
((now - pidtime >= 3600)) && { kill $i; sleep 1; kill &>/dev/null -9 $i; }
done
and the crontab :
crontab -e
0 * * * * /path/to/the/script.bash
回答2:
Solved with:
/bin/ps -Ao"command,pid,ppid"|/bin/grep ' 1$'|/bin/grep /php|/bin/awk '{ print $2; }'|/usr/bin/xargs --no-run-if-empty kill -9
来源:https://stackoverflow.com/questions/12765850/bash-script-to-kill-php-process-older-then-an-hour