init.d脚本重定向可执行文件的stdin标准错误,得到错误的进程ID



我有一个简单的python脚本test.py:

#!/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,因为我的真实脚本是在服务器上打开端口)。的相关部分。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

在过去的两个小时里,我一直在为这件事绞尽脑汁。有人有什么想法吗?

您不应该在初始化脚本中使用sudo。您不应该在任何使用#!/bin/sh的脚本中使用&>。你可能不应该在初始化脚本中使用$!,我认为你对它的使用是错误的。

我建议使用start-stop-daemon使一切更简单,特别是PID的东西,但我不确定你是否可以使用它与重定向输出到日志文件。

相关内容

最新更新