25678909 发表于 2013-08-26 17:09

求助,这样的脚本这么写

就是写一个脚本隔半小时ps HPUNIX服务器中的进程,发现睡眠(s)的进程就自动kill,如果可能的话,脚本要能扫描几个指定进程。哪位高人给指点一下哦,谢谢了!

Shell_HAT 发表于 2013-08-27 08:24

请把你的ps命令的结果(要包含s的)贴出来看看,我可以帮忙写脚本。

hanfeng122525 发表于 2013-08-27 12:42

本帖最后由 hanfeng122525 于 2013-08-27 12:53 编辑

#!/bin/bash

ps aux >> stat

while read line
do
            if [ `echo $line|awk '{print $8}'` = "S" ]
            then
                         pid=`echo $line|awk '{print $2}'`
                         kill $pid
            fi
done < stat


计划任务;;corntab

或者



while true
do
          ps aux >> stat

             while read line
             do
                     if [ `echo $line|awk '{print $8}'` = "S" ]
                     then
                            pid=`echo $line|awk '{print $2}'`
                            kill $pid
                     fi
             done < stat

sleep 1800
done

lyvan 发表于 2013-08-27 17:08

围观看高手解答!!!

25678909 发表于 2013-08-27 20:40

回复 2# Shell_HAT

一次ps查询状态为?的进程结果:

xry      45070.80.13336720 pts/2    ?   17:38   0:00./aps_ora_A
xry      45100.70.0   0    0 pts/2   ?   17:38   0:02    ./bps3
xry      45130.50.0   0    0 pts/2   ?    17:38   0:03    ./app1
xry       4432   0.50.0   0    0 pts/2   ?    17:38   0:03   ./app2
xry       4465   0.50.0   0    0 pts/2    ?    17:38   0:03./bps5
xry       4432   0.40.0   0    0 pts/2    ?    17:38   0:03./app1
xry       4413   0.50.0   0    0 pts/2    ?    17:38   0:03./aps_ora_A
xry       4232   0.20.0   0    0 pts/2    ?    17:38   0:03./aps_send_B
xry       4122   0.20.0   0    0 pts/2    ?    17:38   0:03./aps_send_D
xry       4772   0.10.0   0    0 pts/2    ?    17:38   0:03./aps_send_C

实际是每30分钟发起一次ps,扫描指定的这18个进程 ./aps_ora_A、./aps_ora_B、./aps_ora_C 、./aps_ora_D、./bps1、./bps2、./bps3、./bps4、./bps35、
./app1、./app2、./app3、./app4、./app5、./aps_send_A、./aps_send_B、./aps_send_C、./aps_send_D是否状态为?,用户是xry,如果扫描到则自动kill,具体脚本
能帮我写一个不哦?








Shell_HAT 发表于 2013-08-28 14:20

回复 5# 25678909 #!/bin/ksh
set -A arrayProcess ./aps_ora_A ./aps_ora_B ./aps_ora_C ./aps_ora_D ./bps1 ./bps2 ./bps3 ./bps4 ./bps5 \
./app1 ./app2 ./app3 ./app4 ./app5 ./aps_send_A ./aps_send_B ./aps_send_C ./aps_send_D
PS_RESULT=`ps xxxxxxxxxxx PUT YOUR REAL PS COMMAND HERE xxxxxxxxxxx`
for((i=0;i<=17;i++)); do
    echo "$PS_RESULT" | grep "${arrayProcess[$i]}" | awk '{if($8=="?")print "kill -9 "$2}'
    # echo "$PS_RESULT" | grep "${arrayProcess[$i]}" | awk '{if($8=="?")print "kill -9 "$2}' | sh
done

25678909 发表于 2013-08-28 20:13

while true
do
ps -elf >>stat
while read line

do
    if [`echo $line|awk '{print $2}' `="Z"] && [`echo $line|awk '{print $3}' `="xry"]
then
   pid=`echo $line|awk '{print $2}{print $3}'`
kill $pid
fi
done <stat
sleep 60
done

哪位前辈帮我瞧瞧这段错在哪里?($2是进程状态字段,$3是进程所属用户,本意是当查到进程状态为Z且该进程用户为xry时则自动kill掉),谢谢,在线等!

Shell_HAT 发表于 2013-08-29 17:32

回复 7# 25678909


    报错信息是什么?

25678909 发表于 2013-08-29 17:53

本帖最后由 25678909 于 2013-08-29 18:02 编辑

回复 8# Shell_HAT


while true
do
ps -elf >>stat
while read line

do
    if [`echo $line|awk '{print $2}' `="Z"]
then
   pid=`echo $line|awk '{print $2}'`
kill $pid
fi
done <stat
sleep 60
done

我精简改成这样了,显示结果为:
test-k not found
test-k not found
test-k not found
test-k not found
test-k not found
test-k not found
test-k not found
test-k not found
test-k not found
......
test-k not found
test-k not found
test-k not found
test-k not found
test-k not found
test-k not found
test-k not found
......

然后再手动运行ps -el |grep Z,发现Z状态进程都还在,手动kill -9 可以杀掉,说明这段脚本没起作用啊!而且你看显示结果我标红的地方也是不对劲吧?

Shell_HAT 发表于 2013-08-29 18:02

回复 9# 25678909


if [ `echo $line|awk '{print $2}'` == "Z" ]
方括号两边需要空格
等号两边需要空格
页: [1] 2
查看完整版本: 求助,这样的脚本这么写