How to run something when the system starts

Share your own howto's etc. Not for support questions!

How to run something when the system starts

Postby lacek » 2007-09-12 13:25

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)
lacek
Moderator Team Member
 
Posts: 769
Joined: 2004-03-11 18:49
Location: Budapest, Hungary

Re: How to run something when the system starts

Postby iusumon » 2009-07-05 10:04

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
iusumon
 
Posts: 4
Joined: 2009-05-11 12:29
Location: Bangladesh

Re: How to run something when the system starts

Postby dbbolton » 2009-07-14 11:38

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
dbbolton
 
Posts: 2133
Joined: 2007-06-20 08:17
Location: Iapetus

Re: How to run something when the system starts

Postby bugsbunny » 2009-07-14 21:34

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.
User avatar
bugsbunny
 
Posts: 5355
Joined: 2008-07-06 17:04


Return to Docs, Howtos, Tips & Tricks

Who is online

Users browsing this forum: No registered users and 2 guests

fashionable