First, yes I have seen this question:
The answers there are incorrect and do not work. I have voted and commented accordingly.
The processes I want to kill look like this when listed with ps aux | grep page.py
:
apache 424 0.0 0.1 6996 4564 ? S 07:02 0:00 /usr/bin/python2.6 /u/apps/pysnpp/current/bin/page.py apache 2686 0.0 0.1 7000 3460 ? S Sep10 0:00 /usr/bin/python2.6 /u/apps/pysnpp/current/bin/page.py apache 2926 0.0 0.0 6996 1404 ? S Sep02 0:00 /usr/bin/python2.6 /u/apps/pysnpp/current/bin/page.py apache 7398 0.0 0.0 6996 1400 ? S Sep01 0:00 /usr/bin/python2.6 /u/apps/pysnpp/current/bin/page.py apache 9423 0.0 0.1 6996 3824 ? S Sep10 0:00 /usr/bin/python2.6 /u/apps/pysnpp/current/bin/page.py apache 11022 0.0 0.0 7004 1400 ? S Sep01 0:00 /usr/bin/python2.6 /u/apps/pysnpp/current/bin/page.py apache 15343 0.0 0.1 7004 3788 ? S Sep09 0:00 /usr/bin/python2.6 /u/apps/pysnpp/current/bin/page.py apache 15364 0.0 0.1 7004 3792 ? S Sep09 0:00 /usr/bin/python2.6 /u/apps/pysnpp/current/bin/page.py apache 15397 0.0 0.1 6996 3788 ? S Sep09 0:00 /usr/bin/python2.6 /u/apps/pysnpp/current/bin/page.py apache 16817 0.0 0.1 7000 3788 ? S Sep09 0:00 /usr/bin/python2.6 /u/apps/pysnpp/current/bin/page.py apache 17590 0.0 0.0 7000 1432 ? S Sep07 0:00 /usr/bin/python2.6 /u/apps/pysnpp/current/bin/page.py apache 24448 0.0 0.0 7000 1432 ? S Sep07 0:00 /usr/bin/python2.6 /u/apps/pysnpp/current/bin/page.py apache 30361 0.0 0.1 6996 3776 ? S Sep09 0:00 /usr/bin/python2.6 /u/apps/pysnpp/current/bin/page.py
I'm looking to setup a simple daily cron that will find and kill any page.py
processes older than an hour.
The accepted answer on the aforementioned question does not work, as it doesn't match a range of times, it simply matches processes that have been running from 7 days to 7 days 23 hours 59 minutes and 59 seconds. I don't want to kill processes that have been running from 1-2 hours, but rather anything greater than 1 hour.
The other answer to the aforementioned question using find
does not work, at least not on Gentoo or CentOS 5.4, it either spits out a warning, or returns nothing if the advice of said warning is followed.
GNU Killall can kill processes older than a given age, using their processname.
find doesnt always work, not every system has etimes available, and it might be my regex newb status, but I dont think you need anything more than this:
you can then pipe that to kill or whatever your need may be.
Thanks to Christopher's answer I was able to adapt it to the following:
-mmin
was the find command I was missing.If you want you can feed
ps
with list of PIDs to lookup within, for e. g.:I think you can modify some of those previous answers to fit your needs. Namely:
Or
I think the second may best fit your needs. The find version would wind up nuking other processes by that user
--Christopher Karel
The problem
Converting
etime
(elapsed time) column ofps
command to seconds. The time specification is in this format[[dd-]hh:]mm:ss
. Newer versions ofps
have anetimes
column which outputsetime
value in seconds.The solution: simple custom awk function
This custom awk function supports all formats of
etime
column (e.g.03-12:30:59
,00:07
etc.). Just paste it in your awk script, it is one-liners friendly solution.sec(T)
converts T to secondsT
time specification in[[dd-]hh:]mm:ss
format (e.g.etime
)C
count of fields inT
(equivalent to awk's NF variable)A
array of fields inT
(equivalent to awk's $ variable)A[C>3?C-3:99]
this is safe way to reference the fourth value (i.e. number of days) in reverse order. This approach is useful because days and hours are optional. If the array is not long enough it dereferenceA[99]
which will yield0
value. I assume99
is high enough for most use cases.Real world example
This bash oneliner will kill
soffice.bin
process running under current user if the process is older than 180 seconds.The
lstart
field inps
gives a consistent time format which we can feed todate
to convert to seconds since the epoch. Then we just compare that to the current time.this should work
killall --older-than 1h $proc_name
I modified the answer they gave you in previous post
The regular expression searches for 2 types of second argument:
Hours:minutes:seconds
expression.That should match everything except young processes who would have the form
minutes:seconds
.