Ubuntu – Init.d Script Not Running on Boot

init-scriptinit.dshell-scriptUbuntu

My script is not running on boot in a vagrant box under Ubuntu.

My script looks like this –

#!/bin/bash
# /etc/init.d/mailcatcher
### BEGIN INIT INFO
# Provides: scriptname
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
mailcatcher --http-ip 192.168.50.10

My permissions on the file look like this –

-rwxr-xr-x 1 root root 352 Apr 30 09:59 mailcatcher.sh

I run the command –

sudo update-rc.d "mailcatcher.sh" defaults

If I run the script manually, it works and starts mailcatcher. If I reboot the computer, the mailcatcher daemon does not start. Am I missing something?

Best Answer

And now for the Ubuntu answers.

This is an Ubuntu Linux question, and version 15 is now released. The Ubuntu world now has systemd. But even before version 15 the Ubuntu world had upstart. There really isn't a reason to write System 5 rc scripts; and there is certainly no good reason for starting from there.

Both upstart and systemd do all of the "service controls". All that you need to do is describe the service.

systemd

A systemd service unit, to be placed in /etc/systemd/system/mailcatcher.service, is

[Unit]
Description=Ruby MailCatcher
Documentation=http://mailcatcher.me/

[Service]
# Ubuntu/Debian convention:
EnvironmentFile=-/etc/default/mailcatcher
Type=simple
ExecStart=/usr/bin/mailcatcher --foreground --http-ip 192.168.50.10

[Install]
WantedBy=multi-user.target

This automatically gets one all of the systemd controls, such as:

  • systemctl enable mailcatcher.service to set the service to be auto-started at boot.
  • systemctl preset mailcatcher.service to set the service to be auto-started at boot, if the local policy permits it.
  • systemctl start mailcatcher.service to start the service manually.
  • systemctl status mailcatcher.service to see the service status.

upstart

Upstart is similar, and modifying Fideloper LLC's upstart job file to this question gives this for /etc/init/mailcatcher.conf:

description "Mailcatcher"

start on runlevel [2345]
stop on runlevel [!2345]

respawn

exec /usr/bin/mailcatcher --foreground --http-ip=192.168.50.10

This automatically gets one all of the upstart controls, such as:

  • initctl start mailcatcher to start the service manually.
  • initctl status mailcatcher to see the service status.

Bonus daemontools section

For kicks, for the entertainment of any daemontools-family-using people who reach this via a WWW search, and to demonstrate another reason why not to begin at System 5 rc scripts, I ran that systemd service unit through the nosh toolset's convert-systemd-units command to produce the following daemontools-family run script:

#!/bin/nosh
#Run file generated from ./mailcatcher.service
#Ruby MailCatcher
chdir /
read-conf --oknofile /etc/default/mailcatcher
/usr/bin/mailcatcher --foreground --http-ip 192.168.50.10

Actually, the convert-systemd-units command generates a whole nosh service bundle directory. With that directory, which specifies dependency and ordering information, installed as /var/sv/mailcatcher in a system with the nosh service-manager one gets all of the nosh controls, such as:

  • system-control enable mailcatcher.service to set the service to be auto-started at boot.
  • system-control start mailcatcher.service to start the service manually.
  • system-control status mailcatcher.service to see the service status.
  • system-control preset mailcatcher.service to set the service to be auto-started at boot, if the local configuration (systemd-style presets or /etc/rc.conf{,.local}) permits it.

Don't even begin with System 5 rc files.

Look at this template used by SaltStack for System 5 rc scripts. Even with the SaltStack parameterization eliminated that is 59 lines of shell script code, most of which is generic boilerplate that you'd be having to re-invent and re-write. Again. And Celada has already pointed out where you've re-invented it badly.

The systemd unit file is 11 lines long. The upstart job file is 8 lines. The nosh run script is 6. And they do all of the start/stop/status mechanics for you. Don't start with System V rc, especially not on Ubuntu Linux.

Further reading

Related Question