bash script to kill php process older then an hour

我们两清 提交于 2019-12-11 03:49:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!