0

I need to know which processes are running more than 6 hours in UNIX. How can I find those out?

migrated from serverfault.com Sep 13 '13 at 19:42

This question came from our site for system and network administrators.

  • Care to share what flavor of unix this is? – EEAA Sep 12 '13 at 19:15
  • Most versions of ps aux show the column START with the start time or date. – ott-- Sep 12 '13 at 20:17
  • If you are looking to limit CPU time per process, look at pam_limits. – 200_success Sep 12 '13 at 21:42
2

Depending on what you have available the general approach might be:

ps -o pid,lstart

and run a for loop over the results using something like:

date -j -f %c "$sdate" +%s

to convert the date to a UNIX timestamp. From there something like:

time=$((`date +%s`-`date -j -f %c "$sdate" +%s`))
echo $time

should give you the number of seconds that the process has been running. converting to hours is then trivial.

The short and long is that you will end up writing a script.

0

You can use following function to Get Process elapse time in minutes

GetProcTime() {
    local p=$1
    ps -eao "%C %U %c %t" | 
    awk "/$p/"'{print $4}' | 
    awk -F":" '{{a=$1*60} {b=a+$2}; if ( NF != 2 ) print b ; else print $1 }'
}

Test

root@ubuntu:/tmp# GetProcTime monit
10
root@ubuntu:/tmp# if [[ $(GetProcTime monit) -ge 360 ]]; then echo "Process is running more than 6 hrs"; else echo "OK"; fi
OK
root@ubuntu:/tmp# GetProcTime init
466
root@ubuntu:/tmp# if [[ $(GetProcTime init) -ge 360 ]]; then echo "Process is running more than 6 hrs"; else echo "OK"; fi
Process is running more than 6 hrs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy