2012-02-16 43 views
2

我如何才能找到最近的5个小时内开始处理流程? ps能做到吗?用“PS”找到一个时间范围

我不得不使用ps -ef | grep <username>它显示了所有过程。然后,我必须手动看看STIME列

回答

2

ps -eo etime,pid将列出所有PID以来格式[[DD-]hh:]mm:ss进程创建所经过的时间在一起。这可能会派上用场,因为您可以搜索小于5:00:00的时间量,而不是执行更复杂的日期比较。

+0

+1为简单的方法。但是,因为我想要一种可以在任何时间内工作并且可以排序的解决方案......嗯,请参阅下面(远远低于^^)的文章(仅使用古代,真正古老的系统中可用的东西...)并且这对我所知道的aix&solaris有效) – 2016-03-18 18:13:48

1

stat -t /proc/<pid> | awk '{print $14}'

会给你一个进程的开始时间,以秒以来的时代。与当前时间(date +%s)进行比较,以秒为单位查找过程的年龄。

0

只是为了笑,这里是我不得不对一些古老的系统(包括AIX和Solaris)做的,它似乎只是“OK”到现在为止......(但可以当什么东西在改变失败。未来的同时,我认为它已经失败的机器上持续超过999天凡处理...我会解决这个问题,当我找到一个)

在那些古老的系统:

  • 一些选项ps不存在。 (或者在不同版本的solaris & aix之间有所不同)。
  • 一些死去的过程中必须没有时间,或者还没有用户名,也没有PTY,也没有时间。
  • PS有其ETIME两天有点移位> 99

下面的小东西花费的那些情况下(甚至更多)护理......但实在是太丑了^^


编辑:新版本(有点理智的,用途广泛,但仍显示出你需要处理ps的输出很多得到的东西可用...由于它有改变它显示的信息数量奇怪的是...)

max_size_var_awk="399" #old awk (and nawk) limit of a var size ?! 
ps_output_prettifier="%10s %8s %8s %3s %-14s %11s %10s %s\n" #11s for /dev/pts, as it could also be "zoneconsole" in a solaris zone... 
truncated_mark="(...)" 
size_to_truncate=$((${max_size_var_awk} - ${#truncated_mark})) 
tmpfile= 

#get the ps output 
    ssh localhost "ps -elo user,pid,ppid,nice,etime,tty,time,args" \ 
    | awk '(NF==5) { $8=$5;$7=$4;$6="-";$5="000-00:00:0-";$4=$3;$3=$2;$2=$1;$1="-"; } 
        { print $0}' \ 
    | sed -e 's/^ *//' >"${tmpfile:-/tmp/defaulttmp.$$}" 


    #we read the ps output, putting the first few items in their respective var, and the rest (cmd + args) in "_rest" 
    while read _u _p _pp _n _e _tt _ti _rest ; do 

    #special case: we just read the first line: 
    [ "$_ti" = "TIME" ] && { 
     printf "${ps_output_prettifier}" "$_u" "$_p" "$_pp" "$_n" "99999__$_e" "$_tt" "$_ti" "$_rest" 
     continue 
    } 

    #for all the other lines: 
    </dev/null nawk -v u="$_u" -v p="$_p" -v pp="$_pp" -v n="$_n" -v e="$_e" -v tt="$_tt" -v ti="$_ti" -v template="00000-00:00:00" \ 
     -v rest="$(printf "%s" "$_rest" | cut -c 1-${size_to_truncate})" -v lrest="${#_rest}" -v trunc="${truncated_mark}" -v totrunc="${size_to_truncate}" \ 
     -v ps_output_prettifier="${ps_output_prettifier}" ' 
      BEGIN { rem="we add the beginning of the template to the beginning of the etime column..." 
        prefix=substr(template,1,length(template)-length(e)) ; e=prefix e; 
        rem="and add the message IF it was truncated" 
        if (lrest>totrunc) { rest=rest trunc ; } 
        rem="modify -hh:mm:ss into .hhmmss to allow sort -n to operate on all (and not just on nb-of-days)" 
        sub(/-/,".",e) ; sub(/:/,"",e) ; sub(/:/,"",e) 

        printf (ps_output_prettifier,u,p,pp,n,e,tt,ti,rest);}' 
    done <"${tmpfile:-/tmp/defaulttmp.$$}" \ 
    | sed -e 's/^/ /' | sort -k5,5nr \ 
    | sed -e 's/ \([0-9][0-9][0-9][0-9][0-9]\)\.\([0-9][0-9]\)\([0-9][0-9]\)/ \1-\2:\3:/' 

旧版本:

回旋的sed(如最后一个)尽量保持alignement(和输出更比PS不对齐)。 (如果可以的话,我会使用awk,但是如果更改$ 1,$ 2,...或$ 0中的任何内容重新计算整个行,我不能轻易保留这个对齐方式,也许我应该只做简单的事情并通过更简单的printf重新排列?....我会稍后再做!(但我担心冗长的参数线可能会搞砸了......)并且我不知道如何告诉printf“只是格式化前几个参数,并且将其他所有内容都放在最后的%s中”)

添加回常规ps'时间'会相当一些工作,因为它有1个或2列的过程是否过时的超过24h以下,间距将成为全错了,最后的sed会失败,等等

LC_ALL=C 
ps -eo user,pid,ppid,nice,etime,tty,time,args \ 
| sed -e 's/^\(*[^ ][^ ]* *[^ ][^ ]* *[^ ][^ ]* *[^ ][^ ]*\) \([0-9]-\)/\1 00\2/'    \ 
     -e 's/^\(*[^ ][^ ]* *[^ ][^ ]* *[^ ][^ ]* *[^ ][^ ]*\) \([0-9][0-9]-\)/\1 0\2/'    \ 
     -e 's/^\(*[^ ][^ ]* *[^ ][^ ]* *[^ ][^ ]* *[^ ][^ ]*\) \([0-9][0-9]:\)/\1 000-\2/'  \ 
     -e 's/^\(*[^ ][^ ]* *[^ ][^ ]* *[^ ][^ ]* *[^ ][^ ]*\)  \([0-9][0-9]:\)/\1 000-00:\2/' \ 
     -e 's/^\(*[^ ][^ ]* *[^ ][^ ]* *[^ ][^ ]* *[^ ][^ ]*\)   -/\1 0?_-__:__:__/'  \ 
| sed -e 's/^/ /' | sed -e 's/\([0-9]\)-/\1./' | sed -e 's/\([0-9][0-9]\):\([0-9][0-9]\):/\1\2/'  \ 
| sed -e 's/NI  ELAPSED/NI 999._ELAPSED/'               \ 
| sort -k5,5nr                       \ 
| sed -e 's/\([0-9][0-9][0-9]\)\.\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\) /\1-\2:\3:\4 /'   \ 
| sed -e 's/^ *\([^ ][^ ]*\)\( *\)\([^ ][^ ]*\)\( *\)\([^ ][^ ]*\)\( *\)\([^ ][^ ]*\)\( *\)\([^ ][^ ]*\)$/ ________ \1 \3 \5 0?_-__:__:__  - \7 \9/' 

享受^^ [它仍然是一个工作正在进行中...迄今为止的作品,但我不能尝试在其他系统上(linux?其他版本的aix & solaris等)]