回答
也许这可以帮助你:
做一个PS -aef。这将显示进程 开始的时间。然后使用date命令查找当前时间。计算 两者之间的差异以找出过程的年龄。
归功于:How do you kill all Linux processes that are older than a certain age?
ps -eo etime,pid
将列出所有PID以来格式[[DD-]hh:]mm:ss
进程创建所经过的时间在一起。这可能会派上用场,因为您可以搜索小于5:00:00的时间量,而不是执行更复杂的日期比较。
stat -t /proc/<pid> | awk '{print $14}'
会给你一个进程的开始时间,以秒以来的时代。与当前时间(date +%s
)进行比较,以秒为单位查找过程的年龄。
只是为了笑,这里是我不得不对一些古老的系统(包括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等)]
- 1. 检查时间范围是否在另一个时间范围
- 2. 查找一个日期范围和一个时间范围之间的交集时间
- 3. 一时间范围
- 4. 与给定的时间范围相交查找时间范围
- 5. 检查一个时间范围进入另一个时间范围
- 6. 寻找一个用户界面来选择时间范围
- 7. 子句之间使用,以找到一个范围内的IP
- 8. 找到一个范围内的值:Matlab
- 9. 找到一个范围内的子集
- 10. 找到一个范围内的值
- 11. 找到一个范围内的整数
- 12. wxpython如何找到一个wx.ListCtrl范围
- 13. excel vba,找到一个范围
- 14. 找到一个数字的范围
- 15. 平均在一个时间范围
- 16. 在一个范围内验证时间
- 17. 搜索在一个时间范围
- 18. 一个特定的时间范围
- 19. 创建一个时间范围AVAssetExportSession
- 20. PHP检查时间范围是否在一个时间范围内
- 21. 如何切片日期时间范围与另一个日期时间范围
- 22. 在另一个时间范围内返回数据子集时间范围?
- 23. 查找当前时间是否在一个范围内
- 24. 在另一个时间戳范围内查找带有时间戳范围的所有行
- 25. 如何找到一个范围的日期之间的记录
- 26. 匹配时区到一个特定的时间范围在php
- 27. 如何找出时间范围与C#中给定时间范围的交集?
- 28. C#得到两个日期时间范围之间的时间
- 29. 如何从两个日期时间选择器中找到时间范围?
- 30. 查找重叠的时间范围
+1为简单的方法。但是,因为我想要一种可以在任何时间内工作并且可以排序的解决方案......嗯,请参阅下面(远远低于^^)的文章(仅使用古代,真正古老的系统中可用的东西...)并且这对我所知道的aix&solaris有效) – 2016-03-18 18:13:48