views:

668

answers:

1

I've been tinkering with a regex answer by yukondude with little success. I'm trying to kill processes that are older than 10 minutes. I already know what the process IDs are. I'm looping over an array every 10 min to see if any lingering procs are around and need to be killed. Anybody have any quick thoughts on this?

Thanks, Steve

ps -eo uid,pid,etime 3233332 | egrep ' ([0-9]+-)?([0-9]{2}:?){3}' | awk '{print $2}' | xargs -I{} kill {}

I've been tinkering with the answer posted by yukondude with little success. I'm trying to kill processes that are older than 10 minutes. I already know what the process IDs are. I'm looping over an array every 10 min to see if any lingering procs are around and need to be killed. Anybody have any quick thoughts on this?

Thanks, Steve

+2  A: 

Just like real Zombies, Zombie processes can't be killed - they're already dead.

They will go away when their parent process calls wait() to get their exit code, or when their parent process exits.


Oh, you're not really talking about Zombie processes at all. This bash script should be along the lines of what you're after:

ps -eo uid,pid,lstart |
    tail -n+2 |
    while read PROC_UID PROC_PID PROC_LSTART; do
        SECONDS=$[$(date +%s) - $(date -d"$PROC_LSTART" +%s)]
        if [ $PROC_UID -eq 1000 -a $SECONDS -gt 600 ]; then
            echo $PROC_PID
        fi
     done |
     xargs kill

That will kill all processes owned by UID 1000 that have been running longer than 10 minutes (600 seconds). You probably want to filter it out to just the PIDs you're interested in - perhaps by parent process ID or similar? Anyway, that should be something to go on.

caf
Sorry, i used 'zombie' as its what i've been finding when googling. It's actually part of a daemon that kicks off a PHP scripts that sometimes doesn't exit correctly.
Steve
Ah that works... mostly. I'm on OSX... is there any way to do the date comparison other than with the "date -d"? Mac doesn't allow the -d.
Steve
Well, the problem is that `ps` doesn't really give the elapsed time or process start time in a particularly nice format. It's possible to parse it of course, but it's a pain - the easiest method might be for you to build GNU `date` for your OS X machine and explicitly use that in the script rather than the system's `date`.
caf