Creating an Init Script in Ubuntu 14.04

In Ubuntu, you create init scripts using the SysV init system. Details are here. In this article, I will create a very simple script to start and stop Tomcat.

In this tutorial, our goal is to start and stop Tomcat as the user “joe”. First, we will create a script called tomcat in /etc/init.d folder.

#! /bin/sh
# Provides: tomcat
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Tomcat
# Description: This file starts and stops Tomcat server

export JAVA_HOME=/home/joe/programs/jdk1.8.0_05

case "$1" in
   su joe -c $TOMCAT_DIR/bin/
   su joe -c $TOMCAT_DIR/bin/
   sleep 10
   su joe -c $TOMCAT_DIR/bin/
   sleep 20
   su joe -c $TOMCAT_DIR/bin/
   echo "Usage: tomcat {start|stop|restart}" >&2
   exit 3

The “Default-Start” field specifies the run levels in which the script will be run with the “start” argument. “Default-Stop” does the reverse. For example, when the machines shuts down and enters run level 1, the script will be run with the “stop” argument.

Make the script executable:

sudo chmod a+x tomcat

Always unit test the script by running it from the command line. For example:

sudo ./tomcat start
sudo ./tomcat stop

If all goes well then register the script as an init script:

sudo update-rc.d tomcat defaults

Reboot the machine and make sure that Tomcat has started.

6 thoughts on “Creating an Init Script in Ubuntu 14.04

  1. This is nice!
    Except that it doesn’t work anymore for some new daemon.
    update-rc.d is not valid for the new stuff.
    The rc system actually worked fine until somebody decided to break it.
    Maybe the old SysV worked too well and is too simple to understand.
    I havn’t found a command line which can replace the old update-rc.d

    If somebody could explain how the new system works this would be nice.

    Good article anyway.

  2. I personally use, and believe that start-stop-daemon is a better choice for running an application or script as a certain user.


    #! /bin/sh
    # Provides: Looker
    # Required-Start: $remote_fs $syslog
    # Required-Stop: $remote_fs $syslog
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: Looker
    # Description: Looker

    # DON’T DELETE THE INIT INFO!!!!!!!!!!!!

    # Author:

    # Configuration for this script.

    # Function that starts the daemon/service
    start-stop-daemon –start –background –quiet –pidfile $PIDFILE -c $RUNAS –exec $DAEMON –test > /dev/null \
    || return 1
    start-stop-daemon –start –background –quiet –pidfile $PIDFILE -c $RUNAS –exec $DAEMON — \
    || return 2

    -c is the argument that you use to run as another user. For example: -c root

  3. I think my needs are a bit simpler, but I am unclear on the necessary syntax.
    I am trying to fix a laptop for a friend and have almost everything working as needed, but to enable an older Broadcom B4311 wifi adapter, it is requiring entering the following in terminal after every reboot:
    sudo modprobe -r b43
    sudo modprobe b43
    sudo rfkill unblock all
    I am trying to automate this at startup by using an init script, which ideally should not require any input at all (hopefully, not even an su password). I am not certain how to do so.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s