2017-09-13 34 views
-1

无法过滤Etime,请帮助我找出运行超过24小时的进程/命令。

+0

你能粘贴您的使用,以确定它运行多个进程的shell命令比24小时? – LethalProgrammer

+0

https://serverfault.com/q/181477/94862 – nbari

回答

0
ps -ef 

ps -ef等效为ps -eo uid,pid,ppid,c,stime,tty,time,cmd

etime  ELAPSED elapsed time since the process was started, in the 
        form [[DD-]hh:]mm:ss. 
start  STARTED time the command started. If the process was started 
        less than 24 hours ago, the output format is 
        "HH:MM:SS", else it is " <mm dd" (where Mmm is a 
        three-letter month name). See also lstart, bsdstart, 
        start_time, and stime. 
0

你可以尝试这样就知道多久进程已运行。

ps -o etime= -p "your_pid" 

有关国旗-o etime的信息它提供了经过的时间。

这会返回经过时间


或另一种方式是

ps -eo pid,comm,cmd,start,etime | grep -iv <your_pid> 
+1

我还没有更新我的工作代码,请在下面找到: - ps -eo pid,etime,cmd | grep python | awk'substr($ 0,9,1)==“ - ”'| awk {'print $ 1'} – Challa

0

试用一下这个:

ps -eo etimes,cmd | awk '{if ($1 >= 86400) print $2}' 

etimes将返回在几秒钟的时间,因此, ,你可以使用>=所需的时间

您可以扩展此搜索和例如只杀所需的过程:

kill $(ps -eo etimes,pid,cmd | awk '{if ($3 == "sleep" && $1 >= 30) print $2}') 

在这种情况下,它会搜索cmd睡眠,只会杀死它,如果已经运行了多个过程运行时间超过30秒。

要检查cmd开始用绳子,你可以使用:

ps -eo etimes,pid,cmd | awk '{if ($3~/^sle/ && $1 >= 30) print $2} 

$3~/^sle/将检查命令启动sle

希望这可以帮助你或给你一些想法。

+0

想过滤只有一个命令例如: - http – Challa

+0

然后你可以稍后管道'| grep http' – nbari

+0

ps -eo etimes,pid,cmd | grep python | awk'{if($ 1> = 86400)print $ 2}' – Challa

0

使用命令

alp ❱ ps -e -o etime,pid 
    ELAPSED PID 
    03:10:06  1 
    03:10:06  2 
    03:10:06  3 
    03:10:06  5 
    03:10:06  7 
    03:10:06  8 
    03:10:06  9 
    03:10:06 10 
    03:10:06 11 

etimeps在此格式,因为你需要24很长一段时间,我们可以试试(我用03,因为我没有一个24长的时间过程):

alp ❱ ps -e -o etime,pid | grep -P '^ +03:..:..' 
    03:14:16  1 
    03:14:16  2 
    03:14:16  3 
    03:14:16  5 
    03:14:16  7 
    03:14:16  8 
    03:14:16  9 
    03:14:16 10 
    03:14:16 11 

现在我们可以消除(=摆脱)那些时间:

alp ❱ ps -e -o etime,pid | grep -Po '^ +03:..:..\K +\d+' 
    1 
    2 
    3 
    5 
    7 
    8 
    9 
    10 
    11 

,最终这个输出传递给xargs

alp ❱ ps -e -o etime,pid | grep -Po '^ +03:..:..\K +\d+' | xargs -I xxx echo xxx 
1 
2 
3 
5 
7 
8 
9 
10 
11 

因此,你应该在的地方使用正则表达式部分24:..:..killecho