I have a set of processes that run in parallel.
Occasionally some hang around longer than the script is supposed to allow:
$time_start = microtime(true)
To obtain the list of processes named PROCESS_NAME
which are running longer than 30 minutes you
can issue the following awk
command:
ps ax -ocmd,pid,etime | awk '/PROCESS_NAME/{split($(NF),a,":");if(a[1]>30)print}'
To obtain only the pids run
ps ax -ocmd,pid,etime | awk '/PROCESS_NAME/{split($(NF),a,":");if(a[1]>30)print $(NF-1)}'
You can pipe this to xargs kill
, like this:
ps ax -ocmd,pid,etime | awk '/PROCESS_NAME/{split($(NF),a,":");if(a[1]>30)print $(NF-1)}' \
| xargs kill