Script autostart at boot time on Linux

(Just because I came across this several times during the last weeks being asked by Linux newbees. )
If you want to have a script, a command or a program run at autostart on Linux, put it into /etc/rc.local. On my Ubuntu or Mint systems this file has the following content:

#!/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   

If you add a line like echo “`date`: Hello, dude, I am just booting.” > /tmp/bootlog here, the next time your system starts you will find a timestamp followed by the nice greeting inside the file /tmp.bootlog:

Thu Dec  8 16:06:35 CET 2011: Hello, dude, I am just booting.

Mind that this only works on systems that (perhaps you’ll use locate rc.local) to check that) have the relevant init script /etc/init.d/rc.local on their hard drive. My Ubuntu/Mint systems have the test following test included:

do_start() {
        if [ -x /etc/rc.local ]; then
                [ “$VERBOSE” != no ] && log_begin_msg “Running local boot 
scripts (/etc/rc.local)”
                /etc/rc.local
                ES=$?
                [ “$VERBOSE” != no ] && log_end_msg $ES
                return $ES
        fi
}

Basically, this test makes sure that it is executed if it exists. If you have symbolic links like these:

/etc/rc2.d/S99rc.local                                                                                                                       
/etc/rc3.d/S99rc.local                                                                                                                       
/etc/rc4.d/S99rc.local                                                                                                                       
/etc/rc5.d/S99rc.local

then this script will be run always on the start of runlevels 2,3,4 and 5. You can change that behaviour with the command update-rc.d (a very good manpage is available).
About these ads

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: