1

I'm trying to modify this example using some input from here as I want only to stop the specific Python app running as a daemon because there will be others as well on the same server running on Python so don't want kill all python scripts...

The server runs the Amazon Linux which I believe is CentOS.

USER="root"
APPNAME="myPythonApp1"
APPBIN="/usr/bin/python"
APPARGS="/usr/local/sbin/app1/app.py"
LOGFILE="/var/log/$APPNAME/error.log"
LOCKFILE="/var/lock/subsys/$APPNAME"

LOGPATH=$(dirname $LOGFILE)

prog=$APPBIN

start() {
        [ -x $prog ] || exit 5
        [ -d $LOGPATH ] || mkdir $LOGPATH
        [ -f $LOGFILE ] || touch $LOGFILE

        echo -n $"Starting $APPNAME: "
        daemon --user=$USER "$APPBIN $APPARGS >>$LOGFILE &"
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch $LOCKFILE
        return $RETVAL
}

stop() {
        echo -n $"Stopping $APPNAME: "
        pid=`ps -ef | grep '[p]ython $APPARGS' | awk '{ print $2 }'`
        echo $pid
        kill $pid
        sleep 1
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f $LOCKFILE
        return $RETVAL
}

Start works fine, no issue. When I try and stop I get an error:

kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

If I run from the shell the command to get the pid, it works:

ps -ef | grep '[p]ython /usr/local/sbin/app1/app.py' | awk '{ print $2 }'

I get the processid so what am I missing...?

Many thanks in advance!

Kostas
  • 367
  • 1
  • 3
  • 17

1 Answers1

1

You're using single-quotes in your grep expression:

grep '[p]ython $APPARGS'

In single quotes, a variable won't be expanded. You should use double quotes:

grep "[p]ython $APPARGS"
robert
  • 4,612
  • 2
  • 29
  • 39
  • Thanks @robert ! Follow up question if I may... when I stop I don't get the OK as when I start... is this because of the python script? – Kostas May 19 '15 at 23:10
  • Hmmm … could it be something to do with the value of `$RETVAL`? – robert May 19 '15 at 23:17
  • 1
    This may be helpful http://www.linuxquestions.org/questions/linux-newbie-8/service-start-script-doesn't-show-ok-or-failed-820410/ – robert May 19 '15 at 23:21
  • Indeed... killproc though takes a "/full/path/to/executable" and this means all python daemons will be terminated... :( – Kostas May 19 '15 at 23:26
  • 1
    Where there's a will there's a way … – robert May 19 '15 at 23:33
  • 1
    Managed to do it... details in my follow up post: [link](http://stackoverflow.com/questions/30337798/modifying-python-daemon-script-stop-does-not-return-ok-but-does-kill-the-proce) – Kostas May 19 '15 at 23:58