How to Run Scripts at Ubuntu Startup
Server - Ubuntu

 

Understanding how and which scripts start at boot with Ubuntu is a bit challenging. The transition from System-V init scripts to upstart jobs is not complete nor transparent. Until this whole process is clearer to the Linux administrator you will just have to learn how to work with what is available. There are three ways currently to run scripts at startup with Ubuntu.

 

1. System Upstart Jobs

The directory /etc/init.d has links to the Upstart script for applications converted to Upstart. These applications have completed the transformation to the upstart process. Note that only some of the applications in the /etc/init.d directory have been converted as this will be a process. As the list is reviewed it is clear that some applications lie vsftpd have upstart jobs while other applications that do start on boot do not have upstart jobs like apache2.

 

ls -l /etc/init.d

total 180

-rwxr-xr-x 1 root root 1864 2010-10-18 08:36 acct

-rwxr-xr-x 1 root root 7476 2011-02-22 11:32 apache2

-rwxr-xr-x 1 root root 4111 2011-04-18 11:23 apparmor

lrwxrwxrwx 1 root root 21 2011-05-15 18:58 atd -> /lib/init/upstart-job

-rwxr-xr-x 1 root root 2341 2009-09-07 12:58 bootlogd

lrwxrwxrwx 1 root root 21 2011-05-15 19:45 console-setup -> /lib/init/upstart-job

lrwxrwxrwx 1 root root 21 2011-05-15 19:40 cron -> /lib/init/upstart-job

lrwxrwxrwx 1 root root 21 2011-05-15 19:45 dmesg -> /lib/init/upstart-job

-rwxr-xr-x 1 root root 1246 2010-11-04 08:22 dns-clean

lrwxrwxrwx 1 root root 21 2011-05-15 19:46 ecryptfs-utils-restore -> /lib/init/upstart-job

lrwxrwxrwx 1 root root 21 2011-05-15 19:46 ecryptfs-utils-save -> /lib/init/upstart-job

-rwxr-xr-x 1 root root 1105 2010-10-06 06:05 grub-common

-rwxr-xr-x 1 root root 1329 2009-09-07 12:58 halt

lrwxrwxrwx 1 root root 21 2011-05-15 19:43 hostname -> /lib/init/upstart-job

lrwxrwxrwx 1 root root 21 2011-05-15 19:40 hwclock -> /lib/init/upstart-job

lrwxrwxrwx 1 root root 21 2011-05-15 19:40 hwclock-save -> /lib/init/upstart-job

lrwxrwxrwx 1 root root 21 2011-05-15 19:45 irqbalance -> /lib/init/upstart-job

-rwxr-xr-x 1 root root 1798 2011-01-18 21:42 kdump

-rwxr-xr-x 1 root root 774 2011-01-18 21:42 kexec

-rwxr-xr-x 1 root root 1159 2011-01-18 21:42 kexec-load

-rwxr-xr-x 1 root root 1293 2009-09-07 12:58 killprocs

-rwxr-xr-x 1 root root 1914 2011-03-18 09:58 mdadm

lrwxrwxrwx 1 root root 21 2011-05-15 19:40 module-init-tools -> /lib/init/upstart-job

lrwxrwxrwx 1 root root 21 2011-07-12 19:04 mysql -> /lib/init/upstart-job

-rwxr-xr-x 1 root root 2686 2011-02-18 07:26 networking

lrwxrwxrwx 1 root root 21 2011-05-15 19:42 network-interface -> /lib/init/upstart-job

lrwxrwxrwx 1 root root 21 2011-05-15 19:42 network-interface-security -> /lib/init/upstart-job

-rwxr-xr-x 1 root root 882 2009-09-07 12:58 ondemand

lrwxrwxrwx 1 root root 21 2011-05-15 19:42 plymouth -> /lib/init/upstart-job

lrwxrwxrwx 1 root root 21 2011-05-15 19:42 plymouth-log -> /lib/init/upstart-job

lrwxrwxrwx 1 root root 21 2011-05-15 19:42 plymouth-splash -> /lib/init/upstart-job

lrwxrwxrwx 1 root root 21 2011-05-15 19:42 plymouth-stop -> /lib/init/upstart-job

lrwxrwxrwx 1 root root 21 2011-05-15 19:42 plymouth-upstart-bridge -> /lib/init/upstart-job

-rwxr-xr-x 1 root root 5990 2011-05-10 07:08 postfix

-rwxr-xr-x 1 root root 561 2011-02-04 01:42 pppd-dns

lrwxrwxrwx 1 root root 21 2011-05-15 19:41 procps -> /lib/init/upstart-job

-rwxr-xr-x 1 root root 3667 2010-07-25 11:40 psad

-rwxr-xr-x 1 root root 3093 2011-02-10 01:59 quota

-rwxr-xr-x 1 root root 1839 2011-02-10 01:59 quotarpc

-rwxr-xr-x 1 root root 8863 2009-09-07 12:58 rc

-rwxr-xr-x 1 root root 801 2009-09-07 12:58 rc.local

-rwxr-xr-x 1 root root 117 2009-09-07 12:58 rcS

-rw-r--r-- 1 root root 1510 2009-09-07 12:58 README

-rwxr-xr-x 1 root root 639 2009-09-07 12:58 reboot

-rwxr-xr-x 1 root root 4399 2010-06-14 04:10 rsync

lrwxrwxrwx 1 root root 21 2011-05-15 19:45 rsyslog -> /lib/init/upstart-job

-rwxr-xr-x 1 root root 3299 2011-03-28 05:03 sendsigs

lrwxrwxrwx 1 root root 21 2011-05-15 19:45 setvtrgb -> /lib/init/upstart-job

-rwxr-xr-x 1 root root 590 2009-09-07 12:58 single

-rw-r--r-- 1 root root 4271 2009-09-07 12:58 skeleton

-rwxr-xr-x 1 root root 4194 2011-04-02 04:16 ssh

-rwxr-xr-x 1 root root 519 2009-09-07 12:58 stop-bootlogd

-rwxr-xr-x 1 root root 1095 2009-09-07 12:58 stop-bootlogd-single

-rwxr-xr-x 1 root root 700 2011-04-13 12:34 sudo

-rwxr-xr-x 1 root root 295 2011-07-15 11:44 tomcat7

lrwxrwxrwx 1 root root 21 2011-05-15 19:41 udev -> /lib/init/upstart-job

lrwxrwxrwx 1 root root 21 2011-05-15 19:41 udev-fallback-graphics -> /lib/init/upstart-job

lrwxrwxrwx 1 root root 21 2011-05-15 19:41 udev-finish -> /lib/init/upstart-job

lrwxrwxrwx 1 root root 21 2011-05-15 19:41 udevmonitor -> /lib/init/upstart-job

lrwxrwxrwx 1 root root 21 2011-05-15 19:41 udevtrigger -> /lib/init/upstart-job

lrwxrwxrwx 1 root root 21 2011-05-15 19:43 ufw -> /lib/init/upstart-job

-rwxr-xr-x 1 root root 2787 2009-11-05 06:03 umountfs

-rwxr-xr-x 1 root root 2245 2011-03-04 05:18 umountnfs.sh

-rwxr-xr-x 1 root root 2332 2011-03-30 15:29 umountroot

-rwxr-xr-x 1 root root 1997 2009-09-07 12:58 urandom

lrwxrwxrwx 1 root root 21 2011-05-21 04:37 vsftpd -> /lib/init/upstart-job

lrwxrwxrwx 1 root root 21 2011-05-23 08:13 xinetd -> /lib/init/upstart-job

 

 

 

 

 

 

 

 

 

 

 

2. Backward Compatible System-V

Place the script you want to run in the /etc/init.d directory and make the script executable.

chmod 755 myscript

 

Once that is done create a symbolic link in the run level directory you would like to use, for example if you wanted to run a program in the graphical runlevel 2, the default runlevel for Ubuntu, you would place it in the /etc/rc2.d directory. You just cannot place it the directory, you must signify when it will run by indicating the startup with an “S” and the execution order is important. Place it after everything else that is in the directory by giving it a higher number.

If the last script to be run is rc.local and it is named S99rc.local then you need to add your script as S99myscript.

ln -s /etc/init.d/myscript /etc/rc3.d/S99myscript

 

Each backward compatible /etc/rc*.d directory has symbolic links to the /etc/init.d/ directory.

 

ls -l rc2.d/

total 4

-rw-r--r-- 1 root root 677 2011-03-28 14:10 README

lrwxrwxrwx 1 root root 15 2011-05-22 15:39 S00kdump -> ../init.d/kdump

lrwxrwxrwx 1 root root 14 2011-05-21 05:33 S20acct -> ../init.d/acct

lrwxrwxrwx 1 root root 17 2011-05-21 10:33 S20postfix -> ../init.d/postfix

lrwxrwxrwx 1 root root 14 2011-05-23 08:15 S20psad -> ../init.d/psad

lrwxrwxrwx 1 root root 17 2011-07-19 21:28 S20tomcat7 -> ../init.d/tomcat7

lrwxrwxrwx 1 root root 18 2011-05-21 06:54 S21quotarpc -> ../init.d/quotarpc

lrwxrwxrwx 1 root root 15 2011-05-21 15:09 S25mdadm -> ../init.d/mdadm

lrwxrwxrwx 1 root root 15 2011-05-15 19:00 S50rsync -> ../init.d/rsync

lrwxrwxrwx 1 root root 19 2011-05-15 19:00 S70dns-clean -> ../init.d/dns-clean

lrwxrwxrwx 1 root root 18 2011-05-15 19:00 S70pppd-dns -> ../init.d/pppd-dns

lrwxrwxrwx 1 root root 14 2011-07-20 05:56 S75sudo -> ../init.d/sudo

lrwxrwxrwx 1 root root 17 2011-05-21 04:44 S91apache2 -> ../init.d/apache2

lrwxrwxrwx 1 root root 21 2011-05-15 19:02 S99grub-common -> ../init.d/grub-common

lrwxrwxrwx 1 root root 18 2011-05-15 18:41 S99ondemand -> ../init.d/ondemand

lrwxrwxrwx 1 root root 18 2011-05-15 18:41 S99rc.local -> ../init.d/rc.local

 

 

 

 

 

 

There is also a tool to perform this task of linking to the rc2.d directory as you can see listed below.

Here is an example of creating a startup script for tomcat 7. Create a script called tomcat7 in /etc/init.d/. This script will then provide a way to automatically start and stop tomcat. As you view the script you can see that the start-up script is a simple bash shell script that is used with case in order to create options for executing the scripts. So the key words in the script are “start”, “stop” and “restart” which are typical options for starting daemons. The “sh” executes a command to activate one the the scripts that is located in the /usr/share/tomcat7/bin directory. Note that in each option the case option is closed with the “;;”. The “esac” is used to complete the case section and then “exit 0” closes the script. This is a simple way to create your own script to start applications if they do not come with initialization scripts.

 

#!/bin/bash

# Tomcat 7 Start-Up

#


case $1 in

start)

sh /usr/share/tomcat7/bin/startup.sh

;;

stop)

sh /usr/share/tomcat7/bin/shutdown.sh

;;

restart)

sh /usr/share/tomcat7/bin/shutdown.sh

sh /usr/share/tomcat7/bin/startup.sh

;;

esac

exit 0

 

 

 

 

 

 

 

Starting/Stopping/Restarting tomcat7

/etc/init.d/tomcat7 start

/etc/init.d/tomcat7 stop

/etc/init.d/tomcat7 restart

 

Now install an application to automatically start this application in runlevel 2 when the system boots.

sudo apt-get install sysv-rc-conf

Run the script as root and it opens a configuration window. Remember that the default runlevel will be 2 so place an “X” in the options you want to start at boot in that runlevel. You can see that the tomcat7 script is recognized by the command.

 

 

3. Using rc.local

The /etc/rc.local file provides a way for an administrator to initialize a script in order to start an application. By placing a link to the application you want to run in that file it will be executed on boot. Here is an example line for tomcat 7 if you wanted to use this file.

/etc/init.d/tomcat7 start

rc.local

This script is provided so that you may add scripts to start when the system boots. The rc command will activate the s99local request for runlevels 2-5. This is linked symbolically to rc.local.

 

Here is the file which is by default empty.

#!/bin/sh -e

#

# rc.local

#

# This script is executed at the end of each multiuser runlevel.

# Make sure that the script will "exit 0" on success or any other

# value on error.

#

# In order to enable or disable this script just change the execution

# bits.

#

# By default this script does nothing.

 

exit 0

 

Once the rc.local script has executed the system is running.