modifying python daemon script, stop does not work

2019-08-07 17:52发布

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!

1条回答
走好不送
2楼-- · 2019-08-07 18:35

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"
查看更多
登录 后发表回答