Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm making a simple upstart daemon.

Following the Upstart documentation[1] I make the conf script:

description "Super Daemon" 
author "Bastian Baltasar Bux"

start on started dbus
stop on runlevel [06]

expect fork
exec /usr/bin/my-daemon

The script /usr/bin/my-daemon is a bash that call a python. The python script log information in a /var/log file. On that python script, I only make a fork. But when I check the forks like says in the Upstart doc [2], the method says that I do twice forks.

With that all, the start job is executed correctly, but the pid showed with the upstart my-daemon status isn't the same that is in the log file. The error appears when I try to stop doing status my-daemon stop. That command doesn't end, I must to finish with Crtl-C.

I tried to change the expect fork to expect daemon but service stop doesn't ends neither.

When could be the problem?

[1] http://upstart.ubuntu.com/cookbook

[2] http://upstart.ubuntu.com/cookbook/#id158

share|improve this question
    
How about sudo stop my-daemon? –  schkovich Jun 11 '13 at 5:46
    
Doesn't work, neither with initctl stipo my-daemon –  ssoto Jun 11 '13 at 7:34
1  
Any useful info in /var/log/upstart/my-daemon.log? Why do you need a bash script to call Python script? –  schkovich Jun 11 '13 at 9:36
    
@schkovich thank for you question. Changing the line exec /usr/bin/my-daemon to exec python $MY_SCRIPT_PATH solve the issue. I think the problem is in the bash scritp that doesn't fork any time. –  ssoto Jun 11 '13 at 15:36

1 Answer 1

up vote 3 down vote accepted

I noticed you have already fixed the issue by fixing the exec line. For future readers, let's summarize the issue and the fix.

Issue: Running a forking python from a bash script from Upstart job does not get tracked correctly.

Problem: expect fork or expect daemon does not fix the issue.

Solution: Fix the script. Options:

  • Switch from exec /usr/bin/my-bash-wrapper to directly calling python exec python /path/to/script (as demonstrated in comments above)

  • Fix the bash script. Having a bash script with example contents of

#!/bin/bash

python /path/to/script

has the issue of python getting executed separately from bash with a new process id. Thus it is ignored by Upstart as it is just a command parented by the Bash script. Adding expect daemon does not help as it is ignored in the first place.

Using bash script is possible the same way you fixed it in the Upstart script. Prefixing python call with exec makes the python run with same PID as the bash, which is tracked by Upstart. Then expect fork catches the fork() in the Python script, and Upstart is happily tracking the right process.

So, using a bash wrapper of:

#!/bin/bash

exec python /path/to/script

does the same thing.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.