列出运行时间超过2小时的进程

Vin*_*nod 6 solaris opensolaris solaris-zones

如何列出已运行超过 2 小时且已定义名称的进程。这是我尝试过的。

ps -efo pid,comm,etime | grep 'process name' | awk '{print $3}' 
Run Code Online (Sandbox Code Playgroud)

这是针对 Solaris 的。

或者有人可以帮助如何创建一个脚本,如果进程运行时间超过 2 小时,该脚本将发送包含进程 ID 的电子邮件。

小智 11

一个查找已运行超过 2 小时的进程的班轮

ps -e -o pid,etimes,command | awk '{if($2>7200) print $0}'
Run Code Online (Sandbox Code Playgroud)

解释:

ps:处理快照命令

-e:列出所有进程

-o:仅包含指定的列

pid: 进程号

etimes:自进程启动以来经过的时间,以秒为单位

command:命令及其所有参数作为字符串

awk:模式扫描和处理语言

$2:每行的第二个标记(默认分隔符是任意数量的空格)

7200:7200 秒 = 2 小时

$0: awk 中的整行

由于 awk 结构中的默认操作 pattern { action }是打印当前行,因此可以缩短为:

ps -e -o pid,etimes,command | awk '$2 > 7200'
Run Code Online (Sandbox Code Playgroud)

更多的:

man ps
man awk
Run Code Online (Sandbox Code Playgroud)


Mar*_*art -1

我几年前写的一些东西。\n这会查找 progname 变量中列出的应用程序名称,如果早于终止时间变量中的值(以秒为单位),则会终止它。\n您必须更改 progname 以匹配 ps -o comm 返回的进程名称\n您还必须更改killtime 中的值以匹配您想要的秒数。\n可以从 cronjob 中触发来经常检查。

\n\n

在运行之前,请确保您知道所有这些的作用,否则它可能会杀死意外的进程。

\n\n

这在 RHEL 7.x 中有效,因此对 Solaris 不太乐观,但它们非常接近,因此只需很少的调整或无需调整即可工作。\n如果有格式错误,我深表歉意。有一些残留的格式混乱,我必须清理。

\n\n
###With email send on process kill\n#!/bin/bash ############################################################################## #\xc2\xa0 Name: checkRunawaProgram.sh\n #\xc2\xa0 Version: 1.0\n #\xc2\xa0 Date: 10/07/2015\n #\xc2\xa0 Author: Mark S\n #\xc2\xa0 Description: check processes for a named command and if older than a specified time in seconds kill the process.\n #Note about time: If killtime is over 60 seconds it will be off by 40 seconds.\n #\xc2\xa0 Example: ./checkRunawaProgram.sh fire off from cronjob or run manually\n #NOTE: adjust the progname and killtime fields for your file and delay time.\n # and adjust email address to your addr. \n #EDITED By-On-Why\n #Mark-10/08/15-Clean up and add variables progname and killtime\n#Mark-10/9/15 Add email and logger\xc2\xa0\n#\n##############################################################################\nprogname=runawaProgram.sh\nkilltime=50\nps -o uname,pid,etime,comm -C $progname \\\n| while read user pid elapsed comm\n \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 do\n \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 echo etime $elapsed\n \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 echo pid $pid\n \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 #Strip off : from elapsed and store in elapsed1\n \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 elapsed1=${elapsed//[:]/}\n \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 echo elapsed1 $elapsed1\n \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 if [ ${elapsed1} -gt ${killtime} ]\n \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 then\n \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 echo greater than 10 on pid $pid\n \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 echo killing pid $pid\n \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 kill $pid ## ##email Variables\n now=`date`\n subject="es-ppscnftp01 cron killing process\xc2\xa0 $pid"\n varHost=`hostname`\n sendTo="youremail@yourdomain.net"\n mail -s "$subject" "$sendTo" << END_MAIL\n From $varHost\n The process ID $pid was killed\nthe process name was $comm\nEND_MAIL\n#send info to /var/log/messages\n logger The cronjob checkRunawaProgram.sh killed the $pid process for Process name $comm\n fi\n done\n
Run Code Online (Sandbox Code Playgroud)\n