How can I kill the processes matching a grep older than 30 minutes?

后端 未结 1 346
一整个雨季
一整个雨季 2021-01-21 18:47

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)
         


        
1条回答
  •  有刺的猬
    2021-01-21 19:39

    To obtain the list of processes named PROCESS_NAME which are running longer than 30 minutes you can issue the following awkcommand:

    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 
    

    0 讨论(0)
提交回复
热议问题