2014-12-10 36 views
0

我正試圖在Linux服務器上查找進程的運行時間。我使用etime來實現這一點。下面是我使用以秒爲單位的運行時間在linux中的進程

ps -eo etime,args | grep "process"|awk '{print $1}' 

樣本輸出這個

28-08:42:13 

然後我做了以下將此轉換爲秒,併發送自己郵件的代碼時,運行時間少於30 mins(1800 secs)

ps -eo etime,args | grep "process"|awk '{print $1}'|awk -F'[-: ]+' '/:/ {t=$2+60*$1; print t}' 

我看到的問題是,當轉換爲秒,它從運行時間花費$ 2(秒)和$ 1(分鐘),併發送郵件,即使pr已經達到了28-08:42:13。 有沒有更好的方式將運行時轉換爲秒?在運行時間小於1小時的情況下,對第一個輸出使用$ 3和$ 4不起作用。它正在拾取垃圾值並返回與實際運行時間不同的值。 請幫忙。

+0

爲什麼不分析整個時間並獲得unix時間戳?請參閱http://stackoverflow.com/questions/1842634/parse-date-in-bash – ypnos 2014-12-10 21:56:49

回答

2

etime是人類可讀的經過時間。 etimes是機器可讀的時間(以秒爲單位):

$ ps -o etimes,etime,cmd $$ 
ELAPSED  ELAPSED CMD 
515615 5-23:13:35 bash 
+0

ps -eo pid,lstart,cmd – 2015-06-28 11:08:00

0

我使用這個和它的作品。

ps -eo etime,args | grep "process"|awk '{print $1}'|awk -F'[-: ]+' '/:/ && NF==5 {t=$5+60*($4+60*($3+24*($2+365*$1))); print t} /:/ && NF==4 {t=$4+60*($3+60*($2+24*$1)); print t} /:/ && NF==3 {t=$3+60*($2+60*$1); print t} /:/ && NF==2 {t=$2+60*$1; print t}' 
相關問題