G33k Labs
G33k Labs

Reputation: 350

Upstart and init.d priority

I use foreman to run my node.js applications on the production servers (ubuntu server 12). Foreman has a great tool to create scripts for upstart.

The problem is that when I reboot a server, my application (managed by foreman) is launched before redis-server and I've to build some tricks in order to wait for a valid connection.

The ideal solution will be to start redis-server earlier, and hen the node application when all is started.

Boot configuration :

My question is : how would you change the boot order of my node application? I want to wait for redis-server before my application starts but when I do this, it doesn't start :

start on (started redis-server)

I imagine that it's because no events are sent from init.d scripts to upstart but perhaps there is a way I don't know ?

Thanks by advance for your help !

Upvotes: 3

Views: 1469

Answers (2)

Ezequiel Garcia
Ezequiel Garcia

Reputation: 1057

Since this question has no accepted answer, and given I just had the same problem here, I figured I would provide another solution. The question could be re-stated as:

How do I make an upstart job wait on an init.d script?

As the OP says in the question, it's possible to emit an upstart event when the init.d script is started. This way, the upstart job can have a simple start on started SCRIPT_NAME declaration.

In my case, using a custom CentOS-based distribution, my /etc/rc.d/rc is in charge of executing the sysvinit (init.d) scripts. That script is fully upstart aware, and so emits upstart events for each sysvinit script that is started/stopped.

In other words, the /etc/rc.d/rc script has something like this (simplified to leave the juicy stuff):

for i in /etc/rc$runlevel.d/S* ; do
    subsys=${i#/etc/rc$runlevel.d/S??}
    initctl emit --quiet starting JOB=$subsys
    $i start
    initctl emit --quiet started JOB=$subsys
done

I imagine you need to take a look at your scripts and add the event emission where you think it's suitable. In my case, the emission was already there.

You can wait for several events in an upstart job. See this question for how to find out what events are available (I haven't found a better documentation to be honest).

In particular the trick to grep -r emit seems very useful.

Upvotes: 0

Pascal Belloncle
Pascal Belloncle

Reputation: 11389

Perhaps you should have redis be started by foreman instead, so you can better control all the dependencies of your app.

Or make sure foreman start much later than redis (make sure foreman's link in /etc/rc2.d is listed later than S20*.

One more alternative: have redis server also be started by upstart, this is likely going to help upstart manage the dependencies.

There are explanations on how to do this here: https://gist.github.com/bdotdub/714533

And I suggest using "Start must precede another service" instead (http://upstart.ubuntu.com/cookbook/#start-must-precede-another-service) so that redis gets started when you start your own service.

Upvotes: 1

Related Questions