3

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.

Amanada Smith
  • 1,893
  • 9
  • 28
  • 42
  • shouldn't u change your php timeout to something smaller in your php.ini file? – ddoor Oct 07 '12 at 03:18
  • It's not that. I've got it tuned and setup in the config file but sometimes the processes just get lost and sit there for days/weeks taking up ram. It's a known glitch. – Amanada Smith Oct 07 '12 at 03:24
  • ahhh ok, yea i guess it saves going in and doing it manually ha ha :) – ddoor Oct 07 '12 at 04:22

2 Answers2

9

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
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
-1

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
Amanada Smith
  • 1,893
  • 9
  • 28
  • 42