Linux : How to run a command when boots up?

by on July 24, 2005 · 11 comments· LAST UPDATED July 24, 2005

in

Other distribution provided the file called /etc/rc.local but Debian does not use rc.local to customize the boot process. You can use simple method as follows to customize it.

(A) Execute command at system startup
Let us assume you would like to run command called

i) Create a script called mystartup.sh in /etc/init.d/ directory(login as root)
# vi /etc/init.d/mystartup.sh

ii) Add commands to this script one by one:
#!/bin/bash
echo "Setting up customized environment..."
fortune

iii) Setup executable permission on script:
# chmod +x /etc/init.d/mystartup.sh

iv)Make sure this script get executed every time Debian Linux system boot up/comes up:
# update-rc.d mystartup.sh defaults 100

Where,
mystartup.sh: Your startup script name
defaults : The argument 'defaults' refers to the default runlevels, which are 2 through 5.
100 : Number 100 means script will get executed before any script containing number 101. Just run the command ls –l /etc/rc3.d/ and you will see all script soft linked to /etc/init.d with numbers.

Next time you reboot the system, you custom command or script will get executed via mystartup.sh. You can add more commands to this file or even call other shell/perl scripts from this file too.

(B) Execute shell script at system startup
Open the file mystartup.sh in /etc/init.d/ directory
# vi /etc/init.d/ mystartup.sh

Append your script path to the end as follows (suppose your script is /root/fw.start – script that starts firewall)

/root/fw.start

Save the file.

For more info on 'Customizing your installation of Debian GNU/Linux' visit Offical Debian DOC/FAQ

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 11 comments… read them below or add one }

1 Robin July 25, 2005 at 4:10 pm

Just wanna say *THANK you* to all contributeros of this blog and gr8 work. Keep it up guys!

— Robin

Reply

2 Anonymous August 18, 2005 at 2:19 pm

just want to ask…when you use the rc.local, where does the programs or scrits have to be located?

Reply

3 Vivek August 18, 2005 at 11:21 pm

> just want to ask…when you use the rc.local, where does the programs or scrits have to be located?
You have to specify the full path of command or script; you can put them into your own directory or into /etc/init.d directory.
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.

Reply

4 Seig Lionheart August 19, 2005 at 8:02 am

so it can run wherever the script is located as long as the full path is specified? even if it is in /opt/?
ok…thanks…

Reply

5 anil sahajwani December 18, 2008 at 12:16 pm

I am not able to add my scrip at Startup Of fedora using the above steps

My file is in /root/redirect/run.sh

Its content are
iptables -t nat -A OUTPUT -d your_hostname -p tcp –dport 80 -j REDIRECT –to-ports 8080
iptables -t nat -A PREROUTING -d your_hostname -p tcp –dport 80 -j REDIRECT –to-ports 8080

But I am it does nothing ?

Reply

6 Prakash February 16, 2009 at 9:14 am

Just to update.
Ubuntu Intrepid now comes with /etc/rc.local file.

Reply

7 Kunchana August 13, 2009 at 5:10 am

“iv)Make sure this script get executed every time Debian Linux system boot up/comes up:
# update-rc.d mystartup.sh defaults 100″

error: codenumber must be a number between 0 and 99

Reply

8 Luiz Rocha November 16, 2011 at 9:09 pm

Thanks a lot.

Reply

9 Brahma Reddy November 23, 2011 at 3:21 pm

I followed above steps..I am getting the following error at IV) step

If ‘update-rc.d’ is not a typo you can run the following command to lookup the package that contains the binary:
command-not-found update-rc.d

Reply

10 Brahma Reddy November 23, 2011 at 3:22 pm

SUSE Linux Enterprise Server 11 (x86_64)
VERSION = 11
PATCHLEVEL = 0

Reply

11 Davide Ganz February 9, 2012 at 1:20 pm

Very Useful!
Thanks for Sharing :)

Reply

Leave a Comment

Previous post:

Next post: