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 JobsThe 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.
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-VPlace the script you want to run in the /etc/init.d directory and make the script executable.
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.
Each backward compatible /etc/rc*.d directory has symbolic links to the /etc/init.d/ directory.
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.
Starting/Stopping/Restarting tomcat7 /etc/init.d/tomcat7 start
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.localThe /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
# This script is executed at the end of each multiuser runlevel.
Once the rc.local script has executed the system is running. |