8

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!

Troy
  • 183
  • http://askubuntu.com/questions/109516/managing-dependency-across-upstart-and-sysv-style-init-d-script ;) – Rinzwind Apr 12 '13 at 11:20
  • I tried this: start on started rc RUNLEVEL=[2345] but it doesn't help. Elasticsearch wasn't started when my app started. So it appears there isn't a way for my upstart script to know if the services started by init scripts have already started? – Troy Apr 12 '13 at 20:16
  • well 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. – Rinzwind Apr 12 '13 at 20:18
  • See if this answer is helpful! I found upstart script for both :D Untested code so you might need to tweak it for your situation. – Rinzwind Apr 12 '13 at 20:23

2 Answers2

7

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
derabbink
  • 234
3

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

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
Rinzwind
  • 299,756
  • I ended up just creating an init.d script instead. Thanks for your help though. – Troy Apr 12 '13 at 21:55
  • I think you're correct...I just created the init.d script for now as a band-aid due to time constraints. – Troy Apr 12 '13 at 21:58
  • That will work too but is a regression ;) Since 12.10 favors upstart I expected upstart to be the better option. But if you got it working with init script go for it ;) – Rinzwind Apr 12 '13 at 21:58