Operating System - Linux
1752402 Members
5646 Online
108788 Solutions
New Discussion юеВ

Re: starting a script whenever the system reboots

 
SOLVED
Go to solution
Karthik_sg
Frequent Advisor

starting a script whenever the system reboots

Hi all,

I want to know how do we start a shell script when the system reboots.Should we include the path in /etc/init.d or should we include the path in some other place to start the script automatically.Please suggest.

Thanks, in advance
5 REPLIES 5
skt_skt
Honored Contributor
Solution

Re: starting a script whenever the system reboots

Enable a script to start and stop a service at different runlevels using chkconfig
For a script to interact with chkconfig and the automatic starting and stopping of services at different run levels, it needs two key components.

The first is a commented section that allows the script to be managed by the chkconfig tool. Here is an example:

#!/bin/bash

#
# chkconfig 345 10 90
# description This is where you put a description of your service
In this example, the numbers 345 after the chkconfig directive represent the runlevels that the service will be enabled for by default. In this example the service will be enabled by default for runlevels 3, 4, and 5.

The 10 is the start priority. The lower the number the higher the priority and the sooner a service will be started. The 90 is the stop priority. The lower the number the higher the priority and the sooner a service will be stopped when changing runlevels.

The second essential component is that the script must support being called with the arguments "start" and "stop." The script will be called with "start" as an argument when the service is being started at a given runlevel and "stop" when it is being stopped. Within these functions the script should touch a lock file in the /var/lock/subsys/ folder. If this is not done, the script will automatically start, but will not stop automatically. This should be done in the following manner:

start() {
...
touch /var/lock/subsys/servicename

}

stop() {
...
rm -f /var/lock/subsys/servicename
}
Once you have created a script that conforms to these two requirements, place a copy of it into the /etc/init.d/ directory and execute the command:

chkconfig --add servicename
This will register the service and create the necessary links in the appropriate rcX.d directories. You can now administer this service using chkconfig command.
Ivan Ferreira
Honored Contributor

Re: starting a script whenever the system reboots

You can also add it to more /etc/rc.local.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Karthik_sg
Frequent Advisor

Re: starting a script whenever the system reboots

Thanks for the excellent reply.Both the posts proved useful to me
skt_skt
Honored Contributor

Re: starting a script whenever the system reboots

still no points!!!
Karthik_sg
Frequent Advisor

Re: starting a script whenever the system reboots

I have assigned the points,Can u please point me to any useful linux links which talks more about the above query and more .Thanks for everything.