This will send a KILL signal to the oldest (earliest started) apache2:
Code:
env LANG=C LC_ALL=C ps -C apache2 -o pid=,lstart= | bash -c '
OLD=""
CMD=(true)
while read PID DATE ; do
SEC=$[ $(env LANG=C LC_ALL=C date +%s -d "$DATE" 2>/dev/null) -0 ] || continue
if [ -z "$OLD" ] || [ $OLD -gt $SEC ]; then
OLD=$SEC
CMD=(kill -KILL $PID)
fi
done
"${CMD[@]}"
'
It uses Bash, and the
date -d command to parse the date string ps provides. (It seems to be irrespective of locale, but I set the locale anyway.)
If you have runaway apache2 processes, consuming excess amounts of memory, you really should be killing based on that (or better yet, set a RAM limit for apache2 processes). To kill any apache2 that consumes (resident set, in RAM) more than
X kilobytes, you can use
Code:
ps -C apache2 -o rss=,pid= | awk -v limit=X '($1 > limit) { system("kill -KILL " $2) }'