2009-12-05 224 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。哎呀。