Tasks that fork themselves

Asked by Mahesh Asolkar on 2008-10-21

I am trying to setup upstart jobs for icecast2, mpd and mpdscribble. Icecast to start on startup, mpd to start on 'started icecast2' and mpdscribble to start on 'started mpd' and stop on 'stopped mpd'.

I wrote the jobs and they seem to be working as expected. But icecast2 and mpd both fork themselves off. Not sure the term 'fork off' is technically correct, but they go into background and unblock once started.

I think initctl thinks that the jobs are done when this happens. As a result, mpdscribble - which is stop on 'stopped mpd' is killed right away.

How do I handle tasks like icecast2 and mpd that unblock right after start?

Question information

Language:
English Edit question
Status:
Solved
For:
upstart Edit question
Assignee:
No assignee Edit question
Solved by:
Jeff Oliver
Solved:
2008-10-21
Last query:
2008-10-21
Last reply:
2008-10-21
Best Jeff Oliver (jeffrey-oliver) said : #1

If you're using upstart 0.5.0, Try adding the following stanza's to you job file:

session leader
expect fork

If you're not using upstart 0.5.0, restructure your job file:

service

pre-start script
  /etc/init.d/mpd start
end script

post-stop script
  /etc/init.d/mpd stop
end script

Mahesh Asolkar (asolkar) said : #2

Thanks Jeff Oliver, that solved my question.

Mahesh Asolkar (asolkar) said : #3

I am using upstart 0.3.9 that comes with Ubuntu Hardy Heron, so I used your second suggestion. Here's what my mpd job file looks like:
----------------
service

pre-start script
  exec mpd /etc/mpd.conf
end script

post-stop script
  exec mpd --kill
end script
-----------------

I used the actual commands that start and stop mpd. Are there any disadvantages to using this way instead of /etc/init.d/mpd start/stop?

Jeff Oliver (jeffrey-oliver) said : #4

No, they would both effectively accomplish the same thing, i believe.

Using 'service' in you job file, however does have some disadvantages. Upstart cannot track 'service' jobs, due to the fact that they fork themselves. So features like respawn may not work. The solution to that would be to see if the service your attempting to run has a command line flag (or other configuration) that disables the forking done internal to the process.

If so, you can simplify. For instance, my xinetd job file looks like:

# xinetd

start on started network
stop on stopping network

respawn

exec /usr/sbin/xinetd --dontfork -stayalive -pidfile /var/run/xinetd.pid

Mahesh Asolkar (asolkar) said : #5

I did check on the '--dontfork' kind of flag on both icecast2 and mpd. Unfortunately, both of them don't have such an option. At least from the man installed on my system. May be I should open a bug and request that feature!

Thanks,
Mahesh