9/27 Say I want to kill processes older than x hours. If I make a list of PIDs,
check the start time, and kill on that basis, there is a small
possibility that that process will terminate after I've done the check
but before I do the kill, and that another (possibly important) process
will take on that PID, and I would thus end up killing this important
process. Is there any way to avoid this race condition without going down
to kernel level? Thanks.
\- sure, lots of ways. check ppid, argv string, start time of
process, etc. the rightway.easiest way probably depends on
some details of how you are trying to do this but certainly
you can with ps | awk/grep | (xargs|) kill. --psb
\_ well, that mitigates it somewhat further, but does not remove the
essential race condition.
\- yes it does, if you do it correctly. or do you think
something like PAWS isnt good enough either. --psb
\_ no, it doesn't. it's still a race condition. pipes are
not atomic. you should know better. there's still a slim
chance that between the ps and the kill, the proc exits and
another starts. -!op
\_ If you check the start time like the first reply suggests, it
should eliminate the problem.
\_ yes, but what if time wraps around too? :9
\_ No, it's not atomic. Don't they teach Computer Science
anymore? I haven't done real CS in years yet I still know
a race condition when I see one.
\_ There is a race condition, but it doesn't matter. Running kill on
a nonexistant PID doesn't hurt anything, and if you've really got a
system that can wrap around 100k PIDs in the time it takes to run
your ps | ... | kill, you've got one whacked machine. |