Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

How to run something when the system starts

Share your HowTo, Documentation, Tips and Tricks. Not for support questions!.
Post Reply
Message
Author
lacek
Posts: 764
Joined: 2004-03-11 18:49
Location: Budapest, Hungary
Contact:

How to run something when the system starts

#1 Post by lacek »

Ok, so you have something to run at system startup. Here is how to do it.

First, you'll need an "init script". It is called so because it is a shell script most of the time, but can be anything which is executable, even a binary ELF file.
An init script should accept at least the "start" and the "stop" parameters. If the computer starts, the script will be called with the "start" argument, and when the computer stops, it will be called with the "stop" argument. (it is more difficult than this, but you don't need to care for now. If you do, read the man page of init(8))

A skeleton of an init script looks like this:

Code: Select all

#!/bin/bash
case "$1" in
    start)
        # Stuff to do upon startup
    ;;
    stop)
        # Stuff to do upon halt
    ;;
    *)
        echo "Use this way: $0 start|stop"
    ;;
esac
There are a few things:
* The script must exit. The whole bootup process will hang if the script doesn't exit.
* The script must be executable.
* It should print some information, at least a "starting..." message and a "done" when it is finished. Should the script fail, it should definitely print some kind of error message.
* Test your script. Only put it into the /etc/init.d directory if it works from the command line.
* You may have certain environmental variables exported. If the script depends on these, export them from the script itself. For example, if you changed your PATH, the script won't see your PATH when the system boots up, altough it will work if you execute it by hand.
* The scipt must make sure not to do stupid things. For example, if the script called with "start" arguments twice, it should detect that there is nothing to do, and exit without changing anything. It is especially important when you try to manipulate interfaces, devices and the like.

If everything is fine, move your script to the /etc/init.d directory. This directory, by convention, holds the init scripts.
If the script is in place, issue the following command:

Code: Select all

update-rc.d (yourscript) defaults
Here, (yourscript) is the name of your script in the /etc/init.d directory.

If you want to remove a script from the bootup process, you can do it with this command:

Code: Select all

update-rc.d -f (yourscript) remove
Here are some recommended readings (man pages, of course :->):
update-rc.d(8)
init(8)
runlevel(8)

User avatar
iusumon
Posts: 4
Joined: 2009-05-11 12:29
Location: Bangladesh
Contact:

Re: How to run something when the system starts

#2 Post by iusumon »

nice post :)

Could you please give a real example to run a specific program after booting for example: to mount a drive in /mnt folder

User avatar
dbbolton
Posts: 2129
Joined: 2007-06-20 08:17
Location: Iapetus

Re: How to run something when the system starts

#3 Post by dbbolton »

iusumon wrote:nice post :)

Could you please give a real example to run a specific program after booting for example: to mount a drive in /mnt folder
Sounds like a job for fstab.

But if you want to see some examples, check in the /etc/init.d/ directory on Debian. LOOK but don't TOUCH.
GitHub | zsh docs in Letter PDF
Telemachus wrote:Put down the CGI.

User avatar
bugsbunny
Posts: 5354
Joined: 2008-07-06 17:04
Been thanked: 1 time

Re: How to run something when the system starts

#4 Post by bugsbunny »

And for simple things you can use /etc/rc.local
Just add your commands in that file, and make sure that it's marked as executable.

Post Reply