Linux - Shell Programming                                 Home : www.sharetechnote.com

 

 

 

 

Example > Running a program on Boot (Automatic execution of a program during Linux booting)

 

In this example, I will show you an example script that run a program (tightvncserver in this example) while Linux is booting. There are roughly two ways to run a program on Boot. One method is to create a script in init.d directory and the other one is to add shell command in rc.local file. Creating the script in init.d is a nicer and professional way but it is more complicated because it has several requirements you have to follow. So, in this example I will take the simpler way that is using rc.local file.

 

I wrote it in several lines, but it can be 'one line' script or 'one page' script depending on how robustly you want to implement it. For example, in this example the blue parts are the source script to run a program... it can be just a single line script with sudo /usr/bin/tightvncserver :1 .. all the other lines are to print out some informations into a log file so that you can troubleshoot if something happens. In this example, I assumed that the program tightvncserver is already installed on my Linux. If you want to make the script even more robust, you can add more lines to check whether the program is installed or not and make the script even longer and confusing :)

 

In most Linux distribution, rc.local file is in /etc directory (if not, first find where this file is located). Open the file in any text editor you like and add code as highlighted in blue.

 

# following line looks like a comment, but this is not a simple comment. It indicates where your shell is located.

# so you may need to change this line depending on the specific Linux distribution that you are using.

# In my case, I am writing this for Debian running on my Beaglebone board.

# In the original rc.local file, this line used to be #!/bin/sh -e but I removed -e option. If you put -e option, the script

# would quit when an error happens and does not run any script after the error points. If you are running or doing only

# one job here, it may be good to have -e option, but if you want to run many other programs (doing many other

# jobs), -e option would cause problem because all the other jobs after the point of the first error would not get even

# tried

#!/bin/sh

#

# 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.

 

# Following is to print out a message into a file named bootlog. If this file does not exists, echo command create

# the file first and then write the message in it. so you don't need to worry about whether the file exists or not.

echo "$(date) : Running tightVNC Server" >> /tmp/bootlog

 

# Following is the main part. it is to run tightvncserver. Note that I put sudo at the beginning. Since rc.local runs

# before you log into the system, even tough you have a full permission (root account) the program may not

# be excuted without sudo or su.

sudo /usr/bin/tightvncserver :1

 

# Following is to print out the status (success or fail) of the previous execusion into bootlog file for troubleshooting.

# Note that you need to put a space right after '[' and right before ']'.

if [ $? -eq 0 ]; then

   echo "$(date) : tightVNC Server Run : Success " >> /tmp/bootlog

else

   echo "$(date) : tightVNC Server Run : Fail " >> /tmp/bootlog

fi

 

exit 0

 

 

 

Reference :

 

[1]