I have a problem with some zombie-like processes on a certain server that need to be killed every now and then. How can I best identify the ones that have run for longer than an hour or so?
相关问题
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- JQ: Select when attribute value exists in a bash a
- Stop child process when parent process stops
- Error building gcc 4.8.3 from source: libstdc++.so
Using ps is the right way. I've already done something similar before but don't have the source handy. Generally - ps has an option to tell it which fields to show and by which to sort. You can sort the output by running time, grep the process you want and then kill it.
HTH
Found an answer that works for me:
warning: this will find and kill long running processes
(Where user-id is a specific user's ID with long-running processes.)
The second regular expression matches the a time that has an optional days figure, followed by an hour, minute, and second component, and so is at least one hour in length.
For anything older than one day,
will give you the answer, but it drops down to day-precision which might not be as useful.
If you're on linux or another system with the /proc filesystem, In this example, you can only see that process 1 has been running since June 22, but no indication of the time it was started.
will give you a more precise answer. For example, here's an exact timestamp for process 1, which ps shows only as Jun22:
Jodie C and others have pointed out that
killall -i
can be used, which is fine if you want to use the process name to kill. But if you want to kill by the same parameters aspgrep -f
, you need to use something like the following, using pure bash and the/proc
filesystem.This lets you find and kill processes older than
max_age
seconds using the full process name; i.e., the process named/usr/bin/python2 offlineimap
can be killed by reference to "offlineimap", whereas thekillall
solutions presented here will only work on the string "python2".If they just need to be killed:
If you want to see what it's matching
The
-i
flag will prompt you with yes/no for each process match.My version of
sincetime
above by @Rafael S. Calsaverini :This reverses the output fields: elapsed time first, full command including arguments second. This is preferred because the full command may contain spaces.