3

I have an app (in the form of a single binary) that I need to run on one of my servers, and I'd like to be able to control its lifecycle properly. What I'd like is the following:

  • simple one command start/stop/restart
  • as little configuration as possible, I don't want to setup big things like monit, or write 500 lines of shell scripts
  • handle stdout/stderr/failures in a log file
  • manage a pidfile in a way that there only is a pid file if the process is running (this seems to be really hard to do by hand using shell scripts correctly)
  • (optional) be able to do something if the binary crashes
  • (optional) start automatically if the server reboots and/or the application crashes

What I'm looking for is a lightweight solution that ideally works with Ubuntu Server out of the box, without much configuration.

Jakub Arnold
  • 1,744
  • 10
  • 26
  • 33

1 Answers1

3

For Ubuntu, I think Upstart is what you are looking for.

https://askubuntu.com/questions/19320/how-to-enable-or-disable-services This AskUbuntu question has excellent info on getting started with what you are doing.

In a nutshell, copy the following to /etc/init/mybinary.conf.

description "My Binary"
author      "Jakub Arnold <Jakub.Arnold@example.com>"

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

respawn

pre-start script
   RUN_MODE="daemons"

end script

exec mybinary

This should get you going with the following:

  • Service management with 'service mybinary start/stop/status'
  • PID file management
  • respawn on crash
  • run on boot
  • logging under /var/log/upstart/mybinary.log

The Upstart cookbook as all the info you'll need: http://upstart.ubuntu.com/cookbook/

As an aside, I feel I should mention Ubuntu is planning to retire Upstart in favour of Systemd, but no roadmap is laid out.

matt604
  • 146
  • 5