Adding script to run at startup or shutdown in Linux

March 10, 2009 at 3:45 PM | Posted in Linux | 1 Comment

1. Write a script and put it into /etc/init.d/ directory. Your script must have start or stop options or both, depends on when you want to run your script. start is called at reboot or system startup and stop is called at shutdown.
2.  Add the executable permission to your script. Let say your script name is test
# chmod +x test

3. Create symbolic link in /etc/rc#.d where # is runlevel number. To know more about run level check /etc/initab or check init man page (type man init).
For example if you want to run your script at system startup,  create a symbolic link in /etc/rc3.d:

    # cd /etc/rc3.d/
    # ln -s ../init.d/scriptname S##scriptname where ## is order of executing script or you can say starting number. Use some 50 or 60. You should choose this number according to the needs of your application: if you try to make the application start too early, all disks may not be mounted yet, network adapters might not be initialized or some system services might not be started yet.
    the rc script will start all scripts with a capital S in numerical order.
    The shutdown portion is similar. Go to the run level that you want to shutdown at:
    - cd /etc/rc0.d/
    - ln -s ../init.d/scriptname K##scriptname where where ## is order of executing script or you can say starting number. Use some 50 or 60.
    the rc script will shutdown all scripts with a capital K in numerical order.
    sample script

    #!/bin/sh
    # Copy at startup and remove at shutdown
    #
    start() {
    cp /etc/test /var/tmp/
    }
    stop() {
    rm -rf /var/tmp/*
    }
    case “$1″ in
    start)
    start
    ;;
    stop)
    stop
    ;;

    *)
    echo $”Usage: $0 {start|stop}”
    RETVAL=1
    esac
    exit 0
    4) Your process is done. Now you can test your script.

    Few commands for runlevel and init
    1) To switch to particular run level use init
    $ telinit 6

    2) chkconfig is a tool that helps in creating/deleting/changing the symbolic links. It requires that you put two specially-formed comment lines in your application startup/shutdown script:
    #chkconfig –list to check the current startup settings
    # chkconfig: 345 80 20
    The numbers on the chkconfig line:
    - The first number (345) is a list of runlevels you’ll want your application. So it will run at runlevel 3, and 5.
    - 80 is the “starting position” number, meaning that this application will be started rather late in the boot process.
    - 20 is the “shutdown order” number. Unless your application has special requirements, it is recommended that the sum of startup and shutdown order numbers for each application should be 100.

    When you have added the two “magic” comment lines to your startup/shutdown script, just place the script to /etc/init.d and run:
    chkconfig –add
    If you need to temporarily block the system from starting/stopping the application automatically, you can use:
    chkconfig off
    To restore the auto-start function, use:
    chkconfig on

Install packages on Linux…

March 10, 2009 at 12:12 PM | Posted in Linux | Leave a comment

Make and install program from source (tar.gz)

  1. Put the packed file (tar.gz)  in the directory you want it to be installed under.
  2. Open the compressed tar ball   “tar zxvf <packagename>.tar.gz” .  It will create a directory like “packagename-2.3.4″ and place the extracted information here.
  3. Go into the created subdirectory and type “./configure”.
  4. Type “make all”
  5. Type “make install”

RPM (Red Hat Package Manger)  – Most common method of distribution of software for Linux. rpm command provides for installation, removal, upgrade, verification and other management of RPM packages.

1) Install new package – use rpm -i or rpm ihv for verbose information
#rpm -ihv gcc-2.96-113.i386.rpm

2) To upgrade a existing package to newer version -
#rpm -U binutils-2.11.93.0.2-11.i386.rpm

3) To remove a package
#rpm -e binutils-2.11.93.0.2-11.i386.rpm

Frequently used option in uninstall
–nodeps – skips dependency checking
–test – Run through all the motion except for actually installing things. Useful to verify that a package can be uninstalled correctly.

4) Query about package (-q)
-a display a list of all the packages installed on the system  –   rpm -qa
-f filename – display a package that contain file name
-R list packages on which this package depends

To know about any installed package
#rpm -ql <package_name> or rpm -qa|grep <package_name>
To list the files contained in the package
#rpm -qlp binutil.i386.rpm

Setting Environment variable in Linux

March 5, 2009 at 11:10 AM | Posted in Linux | 2 Comments

The following setting is for FC8 Linux. The file name or path may change for different flavor of Linux
For system wide effect , edit /etc/bashrc file and for particular user edit file home/<username>/.bash_profile and add your environment variable. For example, adding JAVA_HOME variable
#export JAVA_HOME=/usr/bin
Then save the file and run it using source command.
#source /etc/bashrc or source /home//.bash_profile
You can test it by
echo $<your_variable_name>

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.

Follow

Get every new post delivered to your Inbox.