2009-12-05 43 views
0

这个shell执行sh文件停止并没有发生之后解释这个问题, ,任何线索,哪里是我的错killall httpd的睡眠过程

它的猎物的httpd,如果有超过10个的睡眠过程,并启动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 

回答

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 

格式化使代码更易读;)

0

我看不出有什么问题。

这条线:

if [ $[HTTP] -lt 10 ]; 

大概应该是:

if [ ${HTTP} -lt 10 ]; 

即使你的工作。

如果将此添加为最后一行,则由于处于无限的while循环中,因此不应该看到它的输出。

echo "At end" 

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

让你的第一行是这样的,当它执行,以帮助你看到它会显示脚本行由行到哪里去错误:

#!/bin/bash -x 
+0

的rueslt谈到这样 + '[' 1 ']' ++ PS auwxf ++的grep的httpd ++的grep -v grep的 ++ WC -l + HTTP = 72 + '[' 72 -lt 10 ']' +睡10 + '[' 1 ']' ++ PS auwxf ++的grep的httpd ++的grep -v grep的 ++ WC -l + HTTP = 70 + '['70 -lt 10']' + sleep 10 +'['1']' ++ ps auwxf ++ grep httpd ++的grep -v grep的 ++ WC -l + HTTP = 67 + '[' 67 -lt 10 ']' +睡10 和不会导致死亡的httpd所有睡眠过程中的任何线索 – user172697 2009-12-05 07:51:24

+0

由于HTTP =例如72,而72不是“-lt”10。你正在测试“小于”('-lt'意思是“小于”),你的评论在两个地方说“小于”,但你的问题说“如果超过10就杀死httpd”。如果您希望以不同的方式工作,您必须更改测试的条件。否则,根据脚本和跟踪输出,它正常工作。 – 2009-12-05 11:05:26

0

当心killall如果你想编写可移植的脚本。这并不意味着每个系统都有同样的事情:在Linux系统中,它意味着“在这样的系统上杀死类似这样的进程”,这意味着“杀死我有权杀死的每一个进程”。

如果以root用户身份运行更高版本,则您要杀的一件事是init。哎呀。