Tuesday, June 28, 2011

Automation in Linux 1.0

Automation is an important ingredient in any robust software. Linux OS provides you with the tools that  can be utilized to automate execution of scripts. Here we will discuss mainly two methods for the auto execution of scripts namely,init.d method and .bashrc method.

init.d method
               Consider that you are required to run a script automatically at the start-up itself. Then you can go for init.d method. init.d is actually a directory  in /etc folder. I will illustrate how to auto run scripts at the start-up through following steps
step1  Put the script you intend to run in to the folder /etc/init.d. You need to be root for doing this. 
Step2 Make the script executable by giving chmod +x Script_Name
Step3 Then issue command update-rc.d Script_Name defaults 99

Step3 requires further explanation. update-rc.d helps in the updation of script links. I will quote the man page here
update-rc.d updates the System V style init script links /etc/rcrunlevel.d/NNScript_Name whose target  is  the  script /etc/init.d/Script_name.   These links are run by init when it changes runlevels; they are generally used to start and stop system services such as daemons.  runlevel is one of the runlevels supported by init, namely, 0123456789S,and NN is the two-digit sequence number that determines where in the sequence init will run the scripts.
When we give defaults with update-rc.d the script will have links at every runlevel. You can see the links in the folders /etc/rc#.d , where # stands for the numbers 0 to 6 which denotes the runlevels. Care should be taken to avoid interactive programs being automated through this method

Tips :- You can make use of a single start-up script that is made to run at start-up by above methods to run
any number of other scripts. For example , in my system I've establised a startup script named My_startup. So when I need new scripts that run automatically on the start-up , all I've to do is to add the particular script to the My_starup script. So it becomes really easy to have new start-up scripts.The My_startup in my system will look something like below :-
Startup_script#1 &
Startup_script#2 &
.Startup_script#n &
Note :  PATH and SHELL variables should be defined in the script. Also & at the end of each line helps in launching the next script without waiting for the previous one to finish        
.bashrc method
                       .bashrc is nothing but a shell script that is run each time you launch the terminal. You can find .bashrc file in your home folder (/home/user/) itself. You can add the scripts/commands that you want to run at the launch of terminal to .bashrc file.In my system I've used it to set variables.

The other tool that can be used for running scripts automatically is the crontab utility. It is so powerful so that we can schedule the script to run at a particular time like a day of the month, every minute,every hour,yearly,monthly.... We will have a detailed discussion on it later.

No comments:

Post a Comment