Upstart script that depends on init.d scripts?

I have an upstart script to start a custom nodejs app. The app depends on couchdb and elasticsearch. couchdb and elasticsearch provide init.d scripts for starting/stopping them. Is it possible to tell my upstart script that couchdb and elasticsearch are dependencies? I tried this in my upstart script but it does not seem to work:

start on (started couchdb and started elasticsearch)

Thanks!


I had the same question, and I also found a different answer. The author lists 4 options to accomplish this, of which I like the first one best:

Use initclt emit myservice-started to signal the completion of your dependent service's startup. In the linked answer, it is suggested to add this line to the end of the dependency service's init.d script, but I prefer a different method. I like to create a new inid.d script named myservice-started that only contains a start section. Using the appropriate commenting style in the file's header, I declare that it depends on $myservice to be started. In the start section, I tell upstart about myservice being started. You can install it with update-rc.d.

I like this solution because it is not intrusive; if an update changes any of the existing init.d scripts, it won't affect these additional scripts. But remember that changes to your upstart scripts are required.

It might look like this:

#!/bin/sh -e

### BEGIN INIT INFO
# Provides:          myservice-started
# Required-Start:    $myservice
# Default-Start:     2 3 4 5
# Short-Description: send upstart signal after starting myservice
# Description:       myservice needs to run before some upstart services can run
### END INIT INFO

. /lib/lsb/init-functions

case "$1" in
    start)
        log_daemon_msg "Signaling myservice started..." "myservice-started"
        initctl emit myservice-started --no-wait
    ;;

    *)
        log_action_msg "Usage: /etc/init.d/myservice-started start"
        exit 1
    ;;
esac

exit 0

Your upstart script waiting for myservice can listen for the myservice-started event:

start on myservice-started

The only thing I know that would work is to create (or search for and install) upstart scripts for both elasticsearch and couchdb so you can use the "start on" option.

Upstart script for couchdb

# couchdb v1.2.0
#
# Custom installation of CouchDB

description     "CouchDB v1.2.0, local"
console output

# start after all filesystems & network interfae are available
start on (local-filesystems and net-device-up IFACE!=lo)
stop on runlevel [!2345]

# set working directory
env COUCHDB_WD="/path/to/build-couchdb/build/bin"
export COUCHDB_WD

# required for erlang
env HOME="/home/user"
export HOME

script
  # modify PATH to hit local couchdb's working directory first
  PATH="$COUCHDB_WD:$PATH"
  #export PATH # not necessary inside script block
  #logger -t $0 "HOME='$HOME'"
  #logger -t $0 "PATH='$PATH'"
  # output couchdb logs to custom location
  #exec >>/home/user/couchdb_local.log 2>&1
  exec couchdb
end script
  • edit the env to what you need;
  • Source: https://stackoverflow.com/questions/11980900/how-to-get-custom-couchdb-to-run-as-upstart-job

Upstart for elasticsearch

# ElasticSearch Service

description     "ElasticSearch"

start on (net-device-up
          and local-filesystems
          and runlevel [2345])

stop on runlevel [016]

respawn limit 10 5

env ES_HOME=/usr/share/elasticsearch/home
env ES_MIN_MEM=256m
env ES_MAX_MEM=2g
env DAEMON="${ES_HOME}/bin/elasticsearch"
env DATA_DIR=/data/elasticsearch/data
env CONFIG_DIR=/etc/elasticsearch

console output

script
  if [ -f /etc/default/elasticsearch ]; then
    . /etc/default/elasticsearch
  fi

  su -s /bin/dash -c "/usr/bin/elasticsearch -f -Des.path.conf=$CONFIG_DIR -Des.path.home=$ES_HOME -Des.path.logs=$LOG_DIR -Des.path.data=$DATA_DIR -Des.path.work=$WORK_DIR" elasticsearch
end script
  • Source: https://gist.github.com/rbscott/1052015
  • Change the env dir to what you need.