使用“ps”查找某个时间范围内的进程

发布于 2025-01-05 21:04:02 字数 123 浏览 0 评论 0原文

我如何找到最近 5 小时内开始处理的内容? ps 可以做到吗?

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

How can I find started processed within last 5 hours? Can ps do that?

I have to use ps -ef | grep <username> which shows all process for . Then I have to manually look at STIME columns

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

握住我的手 2025-01-12 21:04:02

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

ps -eo etime,pid will list all PIDs together with the elapsed time since the process creation in the format [[DD-]hh:]mm:ss. This may come in handy as you can then search for time amounts less than 5:00:00 instead of performing trickier date comparisons.

夜清冷一曲。 2025-01-12 21:04:02

也许这对你有帮助:

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

功劳在于: 你如何杀死所有超过一定年龄的 Linux 进程?

maybe this helps you:

do a ps -aef. this will show you the time at which the process
started. Then using the date command find the current time. Calculate
the difference between the two to find the age of the process.

credit goes to: How do you kill all Linux processes that are older than a certain age?

伪心 2025-01-12 21:04:02

stat -t /proc/; | awk '{print $14}'

将为您提供自纪元以来进程的启动时间(以秒为单位)。与当前时间 (date +%s) 进行比较,以查找进程的生存期(以秒为单位)。

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

will give you the start time of a process in seconds since the epoch. Compare with current time (date +%s) to find the age of a process in seconds.

零崎曲识 2025-01-12 21:04:02

只是为了搞笑,这是我必须在一些古老的系统(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 来重新对齐?......我稍后会这样做!(但我担心长参数行可能会把事情搞砸......而且我不知道如何告诉 printf“只需格式化前几个参数,然后将其他所有内容按原样放置在最后 %s")

添加回常规 ps '时间' 将是一项相当大的工作,因为无论该过程的日期是多于还是少于 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 和 aix)索拉里斯, ETC)]

Just for the laughs, here is what I had to do on some ancient systems (both aix and solaris), and it seems just about "ok" up to now... (but may fail when something changes in the future. Also, I think it already fails on machines where processes last for more than 999 days... I'll fix this when I find one)

On those ancient systems:

  • some options of ps don't exist. (or differ between solaris & aix, or different versions of those).
  • Some defunct process have either no time, or have also no username, nor pty, nor time.
  • ps have a little shift for etime having days>99

The little thing below takes care of those cases (and more) ... but is really ugly ^^


Edit: New version (a bit saner, and versatile, but still show you need to process ps output a lot to get something usable... thanks to it having a weird way of changing the number of information it displays...)

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:/'

Old version:

The convoluted sed (like the last one) try to keep the alignement (and the output is more aligned than what ps does). (I would use awk if I could, but as changing anything on $1, $2, ... or $0 recomputes the whole line, I can't easily keep the alignement. maybe I should just do simple things and process it all via a simpler printf to realign?.... I will do that sometime later! (but I fear that long args lines may mess things up... And I don't know how to tell printf "just format the first few args, and put everything else as-is in the last %s")

Adding back-in the regular ps 'time' would be quite some work, as it have 1 or 2 columns whether the process is dated of more or less than 24h, and the spacing would become all wrong again, and the last sed would fail, etc.

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/'

Enjoy ^^ [and it is still a work-in-progress... works so far, but I couldn't try it on other systems (linux? other versions of aix & solaris, etc)]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文