2014-11-04 132 views
0

我有一個進程腳本,我希望它運行並停止(如果已經運行)一個bash腳本。我試過pkill -x test,但它似乎並不奏效。在任何情況下,我相信我需要一個if聲明,看看是否test.sh正在運行..如何在init.d進程中運行並停止bash腳本?

編輯:在停止我加for i in ps ax | grep'test'| AWK '{打印$ 1}' ; do kill -9 $i; done

,似乎修復它..需要做一些更多的測試

這裏是代碼:

#!/bin/bash 
# chkconfig: 2345 20 80 
# description: Description comes here.... 

# Source function library. 
. /etc/init.d/functions 

prog="test" 
NEWLINE=$'\n' 

start() { 
    STR=$"Starting $prog ${NEWLINE}" 
    echo "$STR" 

    /var/www/html/test.sh 
    # code to start app comes here 
    # example: daemon program_name & 
} 

stop() { 
    STR=$"Stopping $prog ${NEWLINE}" 
    echo "$STR" 

    pkill -x test 
    # code to stop app comes here 
    # example: killproc program_name 
} 

case "$1" in 
    start) 
     start 
     ;; 
    stop) 
     stop 
     ;; 
    restart) 
     ${0} stop 
     sleep 1 
     ${0} start 
     ;; 
    status) 
     ;; 
    *) 
     echo "Usage: $0 {start|stop|status|restart}" 
esac 

exit 0 

什麼想法?

+0

'test'與'test.sh'不完全匹配。 – 2014-11-04 20:46:49

回答

0

也許如果腳本test.sh將文件放入/ var /運行它的pid。你可以使用變量$$。 然後,您可以在init腳本中查找帶有ps和grep的pid進程,並簡化它。