四种UNIX下只杀死别人进程的程序

来源: 作者:otto
  有时你需要程序只在一个TTY上运行,但对操作员来讲,又不可能让他在
命令行上发 ps -ef 命令,然后杀死其他的进程. 在你的菜单上调用下
面的过程 only1tps your_program kill 就可以只杀死其他 TTY 上启
动的 your_program 进程。是不是很方便?

# only1tps - 从 ps -ef 的结果中找出某些进程在其他 tty 是否启动
# 参数为进程程序名(包括路径,可只写前一部分)、list/kill
# (C) M.L.Y 2000.12

# 注意: 直接从 ps -ef 的某一列取 CMD/COMD/COMMAND 会发生问题:
# 当 ps -ef 中某个进程的 TIME 超过 99.xx 时, 输出将后移,
# 所以干脆从TIME的中间开始往后找空格, 之后即是 CMD/COMD/COMMAND

if [ $1 ] ; then
if [ $2 -a $2 = kill ] ; then
KILL=1
else
KILL=0
fi
my_tty=`tty`
tty_name=`expr substr $my_tty 6 6`
# NCR UNIX: ps -t pts/2 -> TTY: pts/2
# ps -ef -> TTY: pts002 or pts/2
if [ `uname` = UNIX_SV ] ; then
tty_name=`echo $tty_name | awk '{i=index($0, "/");\
if (i > 0)\
{printf "%s%03d",substr($0,1,i-1),substr($0,i+1);}\
else {printf "%s",$0;}\
}'`
fi
if [ `uname` = SCO_SV ] ; then
AWK=awk
TIMECOL=50
TTYCOL=34
elif [ `uname` = NonStop-UX ] ; then
AWK=awk
TIMECOL=50
TTYCOL=40
elif [ `uname` = HP-UX ] ; then
AWK=awk
TIMECOL=45
TTYCOL=34
elif [ `uname` = UNIX_SV ] ; then
AWK=nawk
TIMECOL=43
TTYCOL=34
else
echo Unknown UNIX version!!!
AWK=echo
TIMECOL=50
TTYCOL=34
fi
if [ `uname` = UNIX_SV ] ; then
ps -ef|$AWK 'BEGIN {r=0} \
substr(substr($0,'"$TIMECOL"'),index(substr($0,'"$TIMECOL"')," "
)+1)\
~"^'"$1"'" {tty=substr($0, '"$TTYCOL"', 7);i=match(tty, /[a-zA-Z
]/);\
if (i > 0) tty=substr(tty, i);j=index(tty, " ");\
if (j > 0) tty=substr(tty, 1, j-1);\
i=match(tty, /^pts/); killit=0;\
if (i > 0) {j=index(tty,"/");\
if (j > 0) ttyno=substr(tty,j+1)+0;\
else ttyno=substr(tty,4)+0;\
myttyno=substr("'"$tty_name"'",4)+0;\
if (ttyno != myttyno) killit=1;}\
else if (tty != "'"$tty_name"'") killit=1;\
if (killit == 1) \
{r=r+1;print;if ('"$KILL"' == 1) \
{print "kill " $2; system("kill " $2)} } }; \
END {if(r==0) print "其他 tty 未启动该进程!"}'
else
ps -ef|$AWK 'BEGIN {r=0} \
substr(substr($0,'"$TIMECOL"'),index(substr($0,'"$TIMECOL"')," "
)+1)\
~"^'"$1"'" {tty=substr($0, '"$TTYCOL"', 7);i=match(tty, /[a-zA-Z
]/);\
if (i > 0) tty=substr(tty, i);j=index(tty, " ");\
if (j > 0) tty=substr(tty, 1, j-1);\
if (tty != "'"$tty_name"'") \
{r=r+1;print;if ('"$KILL"' == 1) \
{print "kill " $2; system("kill " $2)} } }; \
END {if(r==0) print "其他 tty 未启动该进程!"}'
fi
fi

# End of file.

时间:2001-02-05 23:33 来源: 作者:otto 原文链接

好文,顶一下
(0)
0%
文章真差,踩一下
(0)
0%
------分隔线----------------------------


把开源带在你的身边-精美linux小纪念品
无觅相关文章插件,快速提升流量