2

How can I list processes, with a defined name, that have been running for more than 2 hours. This is what I have tried.

ps -efo pid,comm,etime | grep 'process name' | awk '{print $3}' 

This is for Solaris.

Or can someone help how to create a script that will send an email with the process IDs if there are processes running longer than 2 hours.

  • What do you mean by "longer than 2 hours"? Wall-clock time since the process was started? CPU time consumed? – Andrew Henle Jul 19 '19 at 15:34
  • The paragraph at the end seems like a completely different question. You should ask in a different question. (and remove from here) – ctrl-alt-delor Jul 19 '19 at 16:37
  • The modification time of /proc/[PID] is going to be the time the process started. I tried to see if some variation of find /proc \! -mmin 120 ... would work, but wasn't able to come up with a proper find command that would limit the depth of the search to just the /proc/[PID] level in the limited time I had. Someone with better skill with find can probably solve the problem easily. Note that you will pick up a lot of OS processes that you really don't want to kill. – Andrew Henle Jul 22 '19 at 9:33
0

Something I wrote a couple years ago. This looks for the app name listed in progname variable and will kill it if older than value in killtime variable (in seconds). You will have to change the progname to match the process name returned with ps -o comm You will also have to change the value in killtime to match the number of seconds you want. Can fire from a cronjob to inspect every so often.

Be sure you know what all of this does before you run it or it may kill unexpected processes.

This works in RHEL 7.x so not positive about Solaris but they are very close so it may work with little or no adjustments. If there are formatting errors I apologize. Had some leftover formatting mess I had to clean up.

###With email send on process kill
#!/bin/bash ############################################################################## #  Name: checkRunawaProgram.sh
 #  Version: 1.0
 #  Date: 10/07/2015
 #  Author: Mark S
 #  Description: check processes for a named command and if older than a specified time in seconds kill the process.
 #Note about time: If killtime is over 60 seconds it will be off by 40 seconds.
 #  Example: ./checkRunawaProgram.sh fire off from cronjob or run manually
 #NOTE: adjust the progname and killtime fields for your file and delay time.
 # and adjust email address to your addr. 
 #EDITED By-On-Why
 #Mark-10/08/15-Clean up and add variables progname and killtime
#Mark-10/9/15 Add email and logger 
#
##############################################################################
progname=runawaProgram.sh
killtime=50
ps -o uname,pid,etime,comm -C $progname \
| while read user pid elapsed comm
         do
         echo etime $elapsed
         echo pid $pid
         #Strip off : from elapsed and store in elapsed1
         elapsed1=${elapsed//[:]/}
         echo elapsed1 $elapsed1
         if [ ${elapsed1} -gt ${killtime} ]
         then
         echo greater than 10 on pid $pid
         echo killing pid $pid
         kill $pid ## ##email Variables
 now=`date`
 subject="es-ppscnftp01 cron killing process  $pid"
 varHost=`hostname`
 sendTo="youremail@yourdomain.net"
 mail -s "$subject" "$sendTo" << END_MAIL
 From $varHost
 The process ID $pid was killed
the process name was $comm
END_MAIL
#send info to /var/log/messages
 logger The cronjob checkRunawaProgram.sh killed the $pid process for Process name $comm
 fi
 done
  • 1
    The "something you wrote" is missing? – L. Scott Johnson Jul 19 '19 at 17:26
  • Yes I had serious formatting issues when I tried to post code. Cleaned up and now posted. :D – Mark Stewart Jul 19 '19 at 17:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.