【问题标题】:using "ps" to find processes in a time range使用“ps”查找时间范围内的进程
【发布时间】:2012-02-16 15:19:50
【问题描述】:

我如何才能找到过去 5 小时内开始处理的? ps可以吗?

我必须使用 ps -ef | grep <username> 来显示 .然后我必须手动查看 STIME 列

【问题讨论】:

    标签: linux bash ps


    【解决方案1】:

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

    【讨论】:

    • +1 表示简单的方法。但是因为我想要一个可以在任何天数内工作并且可以排序的解决方案......好吧,请参见下文(远低于^^)以了解我的看法(仅使用古代,真正古老的系统上可用的东西......据我所知,这适用于 aix 和 solaris)
    【解决方案2】:

    也许这对你有帮助:

    执行 ps -aef。这将向您显示该过程的时间 开始了。然后使用 date 命令查找当前时间。计算 两者之间的差异来查找进程的年龄。

    致谢:How do you kill all Linux processes that are older than a certain age?

    【讨论】:

      【解决方案3】:

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

      将为您提供自纪元以来进程的开始时间(以秒为单位)。与当前时间 (date +%s) 比较,以秒为单位查找进程的年龄。

      【讨论】:

        【解决方案4】:

        只是为了笑,这是我在一些古老的系统(包括 aix 和 solaris)上必须做的事情,到目前为止,它似乎还“可以”......(但当未来发生变化时可能会失败. 另外,我认为它已经在进程持续超过 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 (就像最后一个)试图保持对齐(并且输出比 ps 更对齐)。 (如果可以的话,我会使用 awk,但由于更改 $1、$2、... 或 $0 上的任何内容都会重新计算整行,因此我无法轻松保持对齐。也许我应该只做简单的事情并通过更简单的 printf 重新对齐?....我稍后会这样做!(但我担心长 args 行可能会把事情搞砸......而且我不知道如何告诉 printf“只需格式化前几个 args,并且将其他所有内容原样放在最后的 %s")

        添加回常规 ps 'time' 将是一项相当大的工作,因为无论进程的日期是多于还是少于 24 小时,它都有 1 或 2 列,并且间距会再次变得完全错误,最后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 等)]

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-07-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多