Please share command to find any script which is running for more than say 30 minutes. in Linux System
-
1Hi, and welcome to SO. Just so you know, you might consider asking this question at unix.stackexchange.com. Further, this might give you some pointers: linuxcommando.blogspot.com/2008/09/…– DWrightJan 15, 2013 at 8:16
-
1This could be helpful: stackoverflow.com/questions/6134/…– Prakash MurthyJan 15, 2013 at 8:17
2 Answers
the ps
command has an etimes
field, that gives you the time since a given process has been started, in seconds.
the following bash script will output the PIDs of processes that have been running for longer than 30 minutes.
#!/bin/sh
MIN=30
SEC=$((MIN*60))
ps -eo etimes=,pid= | while read sec pid; do
if [ ${sec} -gt ${SEC} ]; then
echo ${pid}
fi
done
-
hi @umlaute, your script gives on my box (standard Ubunut 12.04 setup) a "ERROR: Unknown user-defined format specifier "etimes"". What do I do wrong? Jan 15, 2013 at 10:22
-
ah yes; it seems that only newer versions (>=3.3.0) of procps (actually procps-ng) have the "etimes" field. i'm using debian/wheezy that ships with procps-3.3.3– umläuteJan 15, 2013 at 11:41
Assuming GNU date + ps, this gives you a list of pids older than 30 minutes:
# weird format to match ps -o lstart
cutoff=$(date -d '30 minutes ago' +'%a %b %e %T %Y')
ps -e -o lstart,pid |
awk '$0 < "'"$cutoff"'" {print NF}'
On linux, you can then check whether /proc/$pid/exe resolves to a known interpreter like /bin/sh, /bin/bash or /usr/bin/python - if so, you know it's a script.