Waiting on a process/port/service with a custom Upstart service
Here are a couple Upstart configuration options for waiting on process dependency. These files go in your Upstart /etc/init directory. Not the best way? Let me know!
Wait for a port to open
description "DESCRIPTION"
author "YOUR NAME"
# Start the service on runlevel 2/3/4/5
start on runlevel [2345]
# Stop the service on runlevel 0/1/6
stop on runlevel [016]
script
until nc -z localhost 80; do
echo "Waiting for port 80 to open"
sleep 1
done
# Start the service
exec start-stop-daemon --start --name UNIQUE_SERVICE_NAME --exec /PATH/TO/YOUR/SERVICE
end script
Wait for a process NOT managed by Upstart to start
description "DESCRIPTION"
author "YOUR NAME"
# Start the service on runlevel 2/3/4/5
start on runlevel [2345]
# Stop the service on runlevel 0/1/6
stop on runlevel [016]
script
while [ -z "`pidof apache2`" ]; do
echo "Waiting for apache to start"
sleep 1
done
# Start the service
exec start-stop-daemon --start --name UNIQUE_SERVICE_NAME --exec /PATH/TO/YOUR/SERVICE
end script
Wait for a Upstart managed process to start
Note: This doesn’t mean the process is ready.
description "DESCRIPTION"
author "YOUR NAME"
# Start the service when mysql has started
start on started mysql
# Stop the service on runlevel 0/1/6
stop on runlevel [016]
script
# Start the service
exec start-stop-daemon --start --name UNIQUE_SERVICE_NAME --exec /PATH/TO/YOUR/SERVICE
end script
More Upstart service configuration options: Upstart Intro, Cookbook and Best Practises