Tuesday, September 10, 2013

CentOS - Run a script or command on startup


If you are looking to run a script or set of commans on CentOS starup, you can use /etc/rc.local file. /etc/rc.local is a symbolic link to /etc/rc.d/rc.local file, and it is also a script executed after the initial system startup services have been executed.

A typical rc.local script looks like the following:

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


This "touch /var/lock/subsys/local" basically notifies the system that local has been run once.

To execute scripts or commands:

Simply append the command(s) or scripts you want to run on startup to this script. For example, if I want my bonded interface up on startup, I appened the following command:
/sbin/ifconfig bond0 up

Next time you reboot your system, bond0 interface will be up.

No comments: