假设所有这些环境都是Linux(你没有提到),下面的代码也许可以帮助你。
- 为了确定过程的年龄,我使用了
ps -o etime, user, cmd
- 该脚本接收 2 个参数、天数限制和要搜索的过程
- ps 显示所有进程,无论是否分配了 TTY...
如果您需要使用 TTY 限制进程,请删除x
=>ps a -o ...
- 根据您的环境调整 ssh 命令。
示例如何调用此脚本:bash ./mytest.sh 5 bash
,将显示+5 天的bash会话。
# mytest.sh
#--debug-only--# set -xv
[ $# -ne 2 ] && echo "please inform : <#of_days> <regexp>" && exit 1
# receive the # of days
vLimit=$1
# name of proc to search
vProc=$2
vTmp1=/tmp/tmp.myscript.$$
# With this trap , the temp file will be erased at any situation, when
# the script finish sucessufully or interrupted (kill, ctrl-c, ...)
trap "rm $vTmp1 2>/dev/null ; exit" 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# ps manpage :
# etime ELAPSED elapsed time since the process was started, in the form [[DD-]hh:]mm:ss.
ssh cinacio@jdivm04 "ps ax -o etime,user,command | grep -i '$vProc' " >$vTmp1
while read etime user cmd
do
# if not found the dash "-" on etime, ignore the process, start today...
! echo "$etime" | grep -q -- "-" && continue
vDays=$(echo "$etime" | cut -f1 -d-)
[ -z "$vDays" ] && continue
if [ $vDays -ge $vLimit ]; then
echo "The user $user still running the proc $cmd on the last $vDays days...."
fi
done < $vTmp1
#--debug-only--# cat $vTmp1