Nathaniel Ford
Nathaniel Ford

Reputation: 21220

Returning from bash script that invokes a python script

I have a bash script (Controller.sh) that invokes a python script (MyDaemon.py). The latter takes an argument and a command, and can be invoked from the command line like so:

/usr/bin/python /opt/stuff/MyDaemon.py -p Blue start

or

/usr/bin/python /opt/stuff/MyDaemon.py -p Blue stop

or

/usr/bin/python /opt/stuff/MyDaemon.py -p Blue status

I am attempting to get Controller.sh to invoke MyDaemon.py and then exit with a status. The python script should be kicked off and Controller.sh should return. This is my Controller.sh code:

COLOR=$1
COMMAND=$2

DIRNAME=`dirname $0`
RESULT="/tmp/$COLOR.$COMMAND.result"

# remove any old console output
rm -f $RESULT 2>/dev/null

#start with CPU affinity for anything other than CPU 0.
sudo taskset -c 1-8 /usr/bin/python /opt/stuff/MyDaemon.py -p $COLOR $COMMMAND</dev/null >$RESULT 2>&1

STATUS=$?

# print output
cat $RESULT

# check on success
if [ $STATUS -ne 0 ]
then
        echo "ERROR: $COLOR $COMMAND failed"
        exit 1
fi

Now, if on the command line I invoke Controller.sh blue start it kicks off the python script, but Controller.sh does not return a status. On the other hand, if I run the following it does return:

[nford@myserver]# sudo taskset -c 1-8 /usr/bin/python /opt/stuff/MyDaemon.py -p blue start</dev/null >/tmp/blah.log 2>&1
Started with pid 1326
[nford@myserver]#

I am forced to conclude that there is something about the bash script that is preventing it from returning.

It should be noted that MyDaemon.py does fork processes, which is why I need to redirect output. It should also be noted that I'm lifting the majority of this from another script that does something similar with a php script; some of the meaning I'm fuzzy on (such as STATUS=$?). That said, even if I cut out everything after the sudo taskset invocation line, it still fails to return cleanly. How do I get the bash script to properly execute this command?

Post-Script: I'm a little baffled how this question is 'too specific' and was down-voted/voted to close. In an attempt to be crystal clear; I'm trying to understand the differences in how a forking process script runs in the context of the command line versus a bash script. I've provided a specific example above, but this is a general concept.

UPDATE: This results when I run the script using bash -x, further showing that it dies on the sudo taskset line. The fact it's left off the start command is confusing.

[nford@myserver]# bash -x Controller.sh Blue start
+ COLOR=Blue
+ COMMAND=start
++ dirname Controller.sh
+ DIRNAME=.
+ RESULT=/tmp/Blue.start.result
+ rm -f /tmp/Blue.start.result
+ sudo taskset -c 1-8 /usr/bin/python /opt/stuff/MyDaemon.py -p Blue

UPDATE: bash -x reveals the problem: the start command is not being passed through: a typo in the variable name produces as silent bash error. Takeaway: use bash -x for debugging!

Upvotes: 2

Views: 458

Answers (1)

sotapme
sotapme

Reputation: 4903

Because of your typo - You should use set -u at the top of your scripts, it's a life saver and stops sleepless nights as well as negating the pulling of hair.

set -u would have given you...

myscript.sh: line 11: COMMMAND: unbound variable

Remember you can run scripts like so bash -u myscript.sh arg1 arg2 likewise with -x, they both help it tracking down script issues.

Upvotes: 1

Related Questions