killall httpd用于睡眠过程

时间:2009-12-05 06:57:13

标签: shell

这个shell解释了这个问题, 执行.sh文件后停止并且什么也没发生,任何线索在哪里都是我的错误

如果有超过10个睡眠过程,则杀死httpd并启动httpd,零睡眠过程

#!/bin/bash

#this means loop forever
while [ 1 ];
do HTTP=`ps auwxf | grep httpd | grep -v grep | wc -l`;
#the above line counts the number of httpd processes found running
#and the following line says if there were less then 10 found running
if [ $[HTTP] -lt 10 ];
then killall -9 httpd;
#inside the if now, so there are less then 10, kill them all and wait 1 second
sleep 1;
#start apache
/etc/init.d/httpd  start;
fi;

#all done, sleep for ten seconds before we loop again
sleep 10;done

3 个答案:

答案 0 :(得分:1)

为什么要杀死子进程?如果你这样做,你会杀死所有正在进行的会话设置Web服务器配置以满足您的需求会不会更容易吗?

正如丹尼斯已经提到的,你的脚本应该是这样的:

#!/bin/bash

BINNAME=httpd   # Name of the process
TIMEOUT=10      # Seconds to wait until next loop
MAXPROC=10      # Maximum amount of procs for given daemon

while true
do
        # Count number of procs
        HTTP=`pgrep $BINNAME | wc -l`
        # Check if more then $MAXPROC are running
        if [ "$HTTP" -gt "$MAXPROC" ]
        then
                # Kill the procs
                killall-9 $BINNAME
                sleep 1
                # start http again
                /etc/init.d/httpd start
        fi
        sleep $TIMEOUT
done

格式化使代码更具可读性;)

答案 1 :(得分:0)

我看不出它有什么问题。

这一行:

if [ $[HTTP] -lt 10 ];

应该是:

if [ ${HTTP} -lt 10 ];

即使你的作品。

如果你把它作为最后一行添加,你应该永远不会看到它的输出,因为你处于一个无限的while循环中。

echo "At end"

如果你这样做,那真的很奇怪。

让你的第一行看起来像这样,它会在执行时逐行显示脚本,以帮助你看出它出错的地方:

#!/bin/bash -x

答案 2 :(得分:0)

如果您正在尝试编写可移植脚本,请注意killall。它并不意味着每个系统都是一样的东西:在linux上它意味着“在某些系统上杀死这样命名的进程”意味着“杀死我有权杀死的每个进程”。

如果您以root身份运行更高版本,则您杀死的内容之一是init。糟糕。