2011-04-25 16 views
0

我有一個簡單的Python腳本test.py的init.d腳本無法重定向可執行的標準輸入標準錯誤,並得到了錯誤的進程ID

#!/user/bin/python 
print "Why is it not redirecting?" 

然後我在那裏我試圖做的init.d腳本./test.py &> logfile.log & PID=$!除了發生什麼是PID是錯誤的,它打印到我的shell而不是重定向。我正在測試腳本,將它放在/etc/init.d中並運行sudo service productcrawler-router start(我使用sudo,因爲我的真實腳本正在打開服務器上的端口)。 init.d腳本的相關部分。

case "$1" in 
    start) 
     if [ -f $PIDF ] 
     then 
      echo "$NAME is currently running. Killing running process..." 
      $IEXE stop 
     fi 
     cd $LDIR 
     if [ -f $LDIR/$NAME.log ] ; then 
      echo "File already exist." 
     fi 
     sudo python ./$EXEC &> $LDIR/$NAME.log & MYPID=$! 
     ls -l $LDIR | grep $NAME 
     echo $MYPID 
     ps aux | grep "router" 
     ps aux | grep $MYPID 
     echo "$NAME are now running." 
     ;; 

輸出:

-rw-r--r-- 1 root root  0 2011-04-25 13:57 productcrawler-router.log 
11944 
Why is it not redirecting? 
root  11053 0.0 0.2 20364 5704 pts/2 Sl 13:45 0:00 python ./router.py 
root  11933 2.0 0.0 1896 552 pts/2 S+ 13:57 0:00 /bin/sh /etc/init.d/productcrawler-router start 
root  11948 0.0 0.0 4008 752 pts/2 S+ 13:57 0:00 grep router 
root  11950 0.0 0.0 4008 756 pts/2 S+ 13:57 0:00 grep 11944 
productcrawler-router are now running. 

然後,我有一個腳本init.d中:

#! /bin/sh 
# chkconfig 345 85 60 
# description: startup script for produtcrawler-router 
# processname: producrawler-router 

NAME=productcrawler-router 
LDIR=/etc/productcrawler/services 
EXEC=test.py 
PIDF=/var/run/productcrawler.pid 
IEXE=/etc/init.d/productcrawler-router 

### BEGIN INIT INFO 
# Provides:   productcrawler-router 
# Required-Start: $remote_fs $syslog 
# Required-Stop:  $remote_fs $syslog 
# Default-Start:  5 
# Default-Stop:  0 1 2 3 6 
# Description:  Starts the productcrawler-router service 
### END INIT INFO 

if [ ! -f $LDIR/$EXEC ] 
then 
     echo "$LDIR/$EXEC not found." 
     exit 
fi 

case "$1" in 
    start) 
     if [ -f $PIDF ] 
     then 
      echo "$NAME is currently running. Killing running process..." 
      $IEXE stop 
     fi 
     cd $LDIR 
     if [ -f $LDIR/$NAME.log ] ; then 
      echo "File already exist." 
     fi 
     sudo python ./$EXEC &> $LDIR/$NAME.log & MYPID=$! 
     ls -l $LDIR | grep $NAME 
     echo $MYPID 
     ps aux | grep "router" 
     ps aux | grep $MYPID 
     echo "$NAME are now running." 
     ;; 
    stop) 
     if [ -f $PIDF ] 
     then 
       echo "Stopping $NAME." 
       PID_2=`cat $PIDF` 
       if [ ! -z "`ps -f -p $PID_2 | grep -v grep | grep '$NAME'`" ] 
       then 
         kill -9 $PID_2 
       fi 
       rm -f $PIDF 
     else 
       echo "$NAME is not running, cannot stop it." 
     fi 
     ;; 
    force-reload|restart) 
     $0 stop 
     $0 start 
     ;; 
    *) 
     echo "Use: /etc/init.d/$NAME {start|stop|restart|force-reload}" 
     exit 1 
esac 

我一直在敲打我的頭靠在牆上這個在過去的兩小時。任何人有任何想法?

回答

1

您不應在初始化腳本中使用sudo。您不應在使用#!/bin/sh的任何腳本中使用&>。你可能不應該在初始化腳本中使用$!,我認爲你的使用是錯誤的。

我推薦使用start-stop-daemon使一切變得簡單很多,特別是PID的東西,但我不確定是否可以一起使用它並將輸出重定向到日誌文件。

+0

我從另一個SO問題中複製了大部分問題,謝謝指向我的'start-stop-daemon',它使生活變得更加簡單。 :) – 2011-04-29 20:55:47

相關問題