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!

1
  • Presumably echo $pid is printing a blank … Commented May 19, 2015 at 22:24

1 Answer 1

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"
Sign up to request clarification or add additional context in comments.

6 Comments

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?
Hmmm … could it be something to do with the value of $RETVAL?
Where there's a will there's a way …
Managed to do it... details in my follow up post: link
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.