Bash – inittab respawn and rc.local

bashdaemoninitinit-script

At the moment I have a daemon starting at boot using rc.local by the following line.

su -l user -c '/dir/daemon'

but I would like to start this process as a service with respawn. I found out that it should be added to /etc/inittab with something like

daemon:run-level:respawn:script-to-daemon
  1. which run level should I use?
  2. how do I have it executed as user?
  3. if I'm using inittab should I remove the line in rc.local
  4. is there something like forever that'll do this for me

Best Answer

Forget about /etc/inittab.

If you have Ubuntu with upstart, or one of the systemd operating systems, then your system completely ignores /etc/inittab and it is a complete irrelevance.

Forget about runlevels.

They exist in systemd operating systems, but only as compatibility shims. The systemd documentation states that the concept is "obsolete". If you're starting with this on a systemd operating system, don't start there.

Forget about forever.

If you have a service manager, be it runit, systemd, perp, nosh, upstart, s6, or daemontools-encore, then it's already doing what you think you need forever for.

Take your stuff out of rc.local.

On an upstart or systemd system it's really as much of a compatibility shim as runlevels are.

Put your stuff into an upstart job or a systemd service unit.

The latter would look something like

[Unit]
Description=Start the wibble daemon

[Service]
User=wibble-d
ExecStart=/usr/local/bin/wibbled
Restart=always

[Install]
WantedBy=multi-user.target
Related Question