Monday, February 13, 2012

Setup to run a script at every reboot of Linux

There are various ways to acieve the subject task, the three ways are,

1. Depends a bit what you want to do, you could add a line to crontab such as:
@reboot command_goes_here
using crontab -e as root

2. So you have a script of your own that you want to run at bootup, each time you boot up.

Write a script, put it in the /etc/init.d/ directory. Lets say you called it FOO. You then run
% update -rc.d FOO defaults

You also have to make the file you created, FOO, executable, using
$chmod +x FOO

You can check out
% man update -rc.d for more information.
It is a Debian utility to install scripts. The option “defaults” puts a link to start FOO in run levels 2, 3, 4 and 5. (and puts a link to stop FOO into 0, 1 and 6.)

Also, to know which runlevel you are in, use the runlevel command.

3. Anothe way is, For example if you want to add the following commands to be run every time the Redhat linux server is rebooted then just update the /etc/rc.local file and add the following command line in it. It's as simple as that,

mount -t cifs //192.168.2.143/mydir -o username=imseagle,password=oracle123 /u01/mydir

This example is mounting a windows directory (//192.168.2.143/mydir) on a linux server onto /u01/mydir. So like wise you can add any linux command in this file.

The content of rc.local will look like this,

[imseagle@myserver u01]$ more /etc/rc.local#!/bin/sh
## This script will be executed *after* all the other init scripts.# You can put your own initialization stuff in here if you don't# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

mount -t cifs //192.168.2.143/mydir -o username=imseagle,password=oracle123 /u01/mydir