2017-09-13 104 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