Making scripts run at boot time with Debian

Posted by Steve on Mon 11 Oct 2004 at 13:01

Debian uses a Sys-V like init system for executing commands when the system runlevel changes - for example at bootup and shutdown time.

If you wish to add a new service to start when the machine boots you should add the necessary script to the directory /etc/init.d/. Many of the scripts already present in that directory will give you an example of the kind of things that you can do.

Here's a very simple script which is divided into two parts, code which always runs, and code which runs when called with "start" or "stop".

#! /bin/sh
# /etc/init.d/blah
#

# Some things that run always
touch /var/lock/blah

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting script blah "
    echo "Could do more here"
    ;;
  stop)
    echo "Stopping script blah"
    echo "Could do more here"
    ;;
  *)
    echo "Usage: /etc/init.d/blah {start|stop}"
    exit 1
    ;;
esac

exit 0

Once you've saved your file into the correct location make sure that it's executable by running "chmod 755 /etc/init.d/blah".

Then you need to add the appropriate symbolic links to cause the script to be executed when the system goes down, or comes up.

The simplest way of doing this is to use the Debian-specific command update-rc.d:

root@skx:~# update-rc.d blah defaults
 Adding system startup for /etc/init.d/blah ...
   /etc/rc0.d/K20blah -> ../init.d/blah
   /etc/rc1.d/K20blah -> ../init.d/blah
   /etc/rc6.d/K20blah -> ../init.d/blah
   /etc/rc2.d/S20blah -> ../init.d/blah
   /etc/rc3.d/S20blah -> ../init.d/blah
   /etc/rc4.d/S20blah -> ../init.d/blah
   /etc/rc5.d/S20blah -> ../init.d/blah

If you wish to remove the script from the startup sequence in the future run:

root@skx:/etc/rc2.d# update-rc.d -f  blah remove
update-rc.d: /etc/init.d/blah exists during rc.d purge (continuing)
 Removing any system startup links for /etc/init.d/blah ...
   /etc/rc0.d/K20blah
   /etc/rc1.d/K20blah
   /etc/rc2.d/S20blah
   /etc/rc3.d/S20blah
   /etc/rc4.d/S20blah
   /etc/rc5.d/S20blah
   /etc/rc6.d/K20blah

This will leave the script itself in place, just remove the links which cause it to be executed.

You can find more details of this command by running "man update-rc.d".

Share/Save/Bookmark


Posted by yaarg on Fri 15 Oct 2004 at 07:58
[ Send Message | View Weblogs ]

I do wish Debian would do away with the System V run level scheme, it's all a bit confusing. As I understand it some of the run levels aren't even used?! Gentoo has done away with it already, using "reboot", "nonetwork" etc. instead, it's a bit more intuitive.. as it happens the rc-update tool is much better too...

[ Parent | Reply to this comment ]

Posted by Anonymous on Tue 2 Nov 2004 at 06:55

I could not agree more. I have no idea how exactly are those run-levels interpreted by the system.

[ Parent | Reply to this comment ]

Posted by Anonymous (81.89.xx.xx) on Sat 10 Sep 2005 at 10:07
read the /etc/inittab for more info

[ Parent | Reply to this comment ]

Posted by Anonymous (203.91.xx.xx) on Thu 17 Nov 2005 at 05:42
my script created is not displaying messaegs on console that means the cho command is not dispalying any messges on console.
i am working on solaris 10 x86 .so could u help me in this.

[ Parent | Reply to this comment ]

Posted by Anonymous (213.158.xx.xx) on Mon 30 Oct 2006 at 14:03
Yeah, like you, I can only agree.. That something is "complex" and "inaccessible" does not mean it's good (very often the opposite).
Too bad many other linux-solutions and independent projects in general suffer from the same syndrome... ;/

[ Parent | Reply to this comment ]

Posted by Anonymous (64.41.xx.xx) on Thu 7 May 2009 at 08:38
I'd like to see a less complex scheme too and I'm curious about the stuff you mentioned. In the non-runlevel scheme you mention, how do you get the system into a state where you can run e2fsck, ...

Also, I switch between a dual and single display config for X. How would I do that in your scheme?

Thanks.

[ Parent | Reply to this comment ]

Posted by GecKo (222.154.xx.xx) on Thu 23 Feb 2006 at 04:47
[ Send Message ]
Is it possible to have what ever script is running run from a different user? Or should I incorporate that into the startup script with su?

[ Parent | Reply to this comment ]

Posted by Steve (82.41.xx.xx) on Thu 23 Feb 2006 at 09:35
[ Send Message | View Steve's Scratchpad | View Weblogs ]

It is best to use su as you suggested.

Steve

[ Parent | Reply to this comment ]

Posted by Anonymous (201.147.xx.xx) on Fri 6 Oct 2006 at 01:30
Is there any easy way?

[ Parent | Reply to this comment ]

Posted by Anonymous (203.129.xx.xx) on Tue 21 Nov 2006 at 04:49
you're all so damn ignorant and quick to give up. Learn some skills, init.d and rc levels are so easy, i know a 7 year old with complete mastery of them... get better simpletons?

[ Parent | Reply to this comment ]

Posted by Anonymous (72.166.xx.xx) on Sun 8 Apr 2007 at 23:34
Yeah, why whine about it? If another Distro trips your trigger, by all means go for it. As #10 points out in this thread, there is another way to do it, too.

Typical Linux, multiple ways to obtain the same result. I admit Debian is a bit less than intuitive at times but that is how they keep the riff raff out. ;=>

Cheers.

T

[ Parent | Reply to this comment ]

Posted by Anonymous (199.46.xx.xx) on Wed 20 May 2009 at 12:42
I hope you don't support getting Linux onto every computer because your comment speaks in the other direction. The more intuitive the OS the more adoption there will be and a greater interest in supporting it. Everyone benefits from simplifying. No one really benefits from complexity.

[ Parent | Reply to this comment ]

Posted by Anonymous (216.195.xx.xx) on Sat 6 Jun 2009 at 05:54
As so many people have pointed out - widespread adoption isn't really the point of linux. We build a system we personally like. Different people build systems *they* like. Find a distro or an OS that works for you, or learn the one that works for us.

[ Parent | Reply to this comment ]

Posted by Anonymous (220.245.xx.xx) on Tue 26 Dec 2006 at 05:42
I think it would be better to make one init.d script that just executes a custom script. Gentoo has this, it has an init.d script called local, which just runs everything in /etc/conf.d/local.start and local.stop

If you don't like the old Debian init scripts you could try installing initng, also should improve boot times quite a lot although its mostly used by Gentooers but there seems to be Debian builds (At least some old ones in experimental).

http://www.initng.org/ - Seems down currently

[ Parent | Reply to this comment ]

Posted by Anonymous (203.144.xx.xx) on Sun 27 May 2007 at 03:50
/etc/rc.local is in etch.

[ Parent | Reply to this comment ]

Posted by Anonymous (64.81.xx.xx) on Fri 30 Mar 2007 at 23:07
man 5 crontab
specifically look at @reboot
that is easy and will "make scripts run at boot time" only.

[ Parent | Reply to this comment ]

Posted by Anonymous (209.234.xx.xx) on Mon 12 May 2008 at 22:00
Thanks for the idea..didn't even think of that route :)

and if Gentoo is so GREATE, then why are you even here!?

[ Parent | Reply to this comment ]

Posted by Anonymous (66.185.xx.xx) on Sat 25 Oct 2008 at 15:19
Obviously not to chat with people who can't spell "great".

[ Parent | Reply to this comment ]

Posted by Anonymous (62.80.xx.xx) on Tue 1 Jan 2008 at 20:35
Not working for me :(

[ Parent | Reply to this comment ]

Posted by Anonymous (125.25.xx.xx) on Mon 25 Aug 2008 at 18:44
I want to auto start apache on OpenSolaris10.
Help me please!!!

[ Parent | Reply to this comment ]

Posted by Anonymous (69.9.xx.xx) on Wed 3 Sep 2008 at 19:13
If you are going to spend time learning to administer an OS, OpenSLOWaris is probably a bad pick.

[ Parent | Reply to this comment ]

Posted by Anonymous (80.220.xx.xx) on Thu 18 Sep 2008 at 22:09
The example script is a bit too simple, it doesn't conform to the Debian Policy. See http://www.debian.org/doc/debian-policy/ch-opersys.html#s-sysvini t for how to write correct init.d scripts.

[ Parent | Reply to this comment ]

Posted by Anonymous (74.197.xx.xx) on Tue 13 Jan 2009 at 13:47
if you are looking for a gui tool, look at bum (BootUp-Manager) in the repositories. There is a checkbox to display advanced options like priorities and such.

[ Parent | Reply to this comment ]

Posted by Anonymous (59.162.xx.xx) on Wed 25 Feb 2009 at 12:51

#! /bin/sh
# /etc/init.d/blah
#

# Some things that run always
touch /var/lock/blah

# Carry out specific functions when asked to by the system
case “$1â&aci rc;‚¬Â³ in
start)
HOST=’10.147.25 1.90′
USER=’tritech&A tilde;¢â‚¬â„¢
PASSWD=’tritech ’
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
get candle1.C
END_SCRIPT
;;
stop)
echo “Starting script blah �
;;
*)
exit 1
;;
esac

exit 0

this is my script in which i am trying to copy the file candle1.C from an ftp server at the time of starting the system so i had put this file in /etc/init.d/blah .but it is not working , instead of these ftp commands if am using mkdir d something like command it is working means this script is working but for using an ftp what i have to do , or it is possible to start an ftp connection at the time of init process.plz help me.

[ Parent | Reply to this comment ]


User Login

Username:

Password:

[ Advanced Login ]

Register Account

Related Links

Quick Site Search