4

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?

4
  • Your script does not adhere to the proper protocol of honouring requests such as start, stop, status, reload. It just runs the command. It will start the service even if it is asked to stop the service, reload the service, check its status, etc... You should fix that. Nevertheless, that's not what the problem is.
    – Celada
    Commented Apr 30, 2015 at 17:19
  • You seem to have a naming inconsistency. In the file header you've called it mailcatcher and in the update-rc.d command you've called it mailcatcher.sh. Which is it? You should not name is mailcatcher.sh actually, because the suffix .sh has a special meaning for init.d scripts which it would not be desirable to invoke here.
    – Celada
    Commented Apr 30, 2015 at 17:21
  • I have removed all instances of .sh and made sure it is only showing mailcatcher in all spots now. Commented Apr 30, 2015 at 17:27
  • I have discovered my issue. Commented Apr 30, 2015 at 19:44

2 Answers 2

9

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

1
  • We are running 14.04 of Ubuntu. SystemD is unavailable. I was able to reduce my code down to 2 lines. I also replaced my previous answer by simply using crontab to run a single command on boot. It's much simpler that way. This is a fantastic answer so I am going to accept it for those who are using bleeding edge Ubuntu. Commented May 6, 2015 at 14:51
3

When running a command during boot, we don't necessarily have access to the env for that command. I needed to use the fully qualified name for that command.

Doesn't work on boot

mailcatcher --http-ip 192.168.50.10

Works on boot

/usr/bin/mailcatcher --http-ip 192.168.50.10

My script is now functional but I do plan to add service controls so that start and stop work at a later time.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .