tags:

views:

63

answers:

2

I'm trying to start/stop rsyslog through a python script:

RSYSLOG_INIT_SCRIPT='/etc/init.s/rsyslogd'
subprocess.call([RSYSLOG_INIT_SCRIPT,'stop'])

/etc/init.d/rsyslogd is a regular init script. The problem is that it continues executing this script again and again. (I've added an echo to the script to confirm this). This is the stacktrace when i kill it:

  File "queuerunner.py", line 72, in <module>
    rsysloglauncher.startrsyslog()
  File "/root/logging-server/Logging-server-init/src/initializer/rsyslog/rsysloglauncher.py", line 23, in startrsyslog
    subprocess.call([RSYSLOG_INIT_SCRIPT,"stop"])
  File "/storage/local/python-2.6.4/lib/python2.6/subprocess.py", line 470, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/storage/local/python-2.6.4/lib/python2.6/subprocess.py", line 1157, in wait
    pid, sts = os.waitpid(self.pid, 0)

Help?

A: 

Are you sure the script isn't looping itself? The python code looks like it's just waiting for the sub-process to exit.

Get the PID of the python script, then do:

watch pstree -ap <PID>

Look to see if the PID of the start/stop script is the same - maybe the python code is looping itself for some reason.

If the init.d script PID is constant, then do strace or truss on that PID to see what it's doing.

Douglas Leeder
no, I can execute it myself normally through the terminal.
m2o
Make sure the script sees the 'stop' argument - (I don't know why it wouldn't) - echo out the calling args from your script - and call it once from commandline and once from python script.
monojohnny
+1  A: 

Not sure what is going on, but try creating your shell script like this:

#!/bin/sh
while :
do
    echo "Sleeping..."
    sleep 1
done

Then confirm that your python program when running this script does the same thing.

Then confirm the python call with this script:

#!/bin/sh
echo "I will exit"

See if you can get more output from the original script, create a 'proxy' script - which you will call from your Python script.

#!/bin/sh
/etc/init.s/rsyslogd stop > /tmp/log 2>&1

And edit your original script to produce more output:

#!/bin/sh -xv

I'm assuming it's bourne-shell or bash.

Then call this script : and then check the /tmp/log with a :

tail -f /tmp/log

Also, can you get the :

subprocess.call(...

To return a PID of the process that is created ? If so, then track it using something like:

ps -eaf |grep <PID>

Where should be replaced by the actual PID of course.

Depending on OS, you might also be able to :

truss -o /tmp/truss.out <PID>

If this 'truss.out' continues to fill up with system calls, then you know your shell-script is looping - and then something differs (I'm not sure what yet) between the commandline invocation and the python invocation.

I agree with the other poster : it looks like the script is looping - rather than a problem strictly with the python script.

Another thing to try:

Right at the start of the script, echo out the arguments - you might find that it loops if it cannot get hold of the correct params or something.

monojohnny