0

I have a simple python script test.py:

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

Then I have a init.d script where I'm trying to do ./test.py &> logfile.log & PID=$! except what happens is that the PID is wrong, it prints to my shell instead of redirects. I'm testing the script by placing it in /etc/init.d and running sudo service productcrawler-router start (I'm using sudo because my real script is opening ports on the server). The relevant section of the init.d script.

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."
        ;;

Outputs:

-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.

Then I have an init.d script:

#! /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

I've been banging my head against the wall on this for the past two hours. Anyone got any ideas?

Kit Sunde
  • 35,972
  • 25
  • 125
  • 179

1 Answers1

1

You shouldn't use sudo in an init script. You shouldn't use &> in any script that uses #!/bin/sh. You probably shouldn't use $! in an init script, and I think your usage of it is really wrong.

I recommend using start-stop-daemon to make everything a lot simpler, especially the PID stuff, but I'm not sure if you can use it together with redirecting the output to a logfile.

Arrowmaster
  • 9,143
  • 2
  • 28
  • 25
  • I copied most of it from another SO question, thanks for pointing me to `start-stop-daemon` that does make life much easier. :) – Kit Sunde Apr 29 '11 at 20:55