≡ Menu

Linux : How to run a command when boots up?

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

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 11 comments… add one }

  • Robin July 25, 2005, 4:10 pm

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

    — Robin

  • Anonymous August 18, 2005, 2:19 pm

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

  • Vivek August 18, 2005, 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.

  • Seig Lionheart August 19, 2005, 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…

  • anil sahajwani December 18, 2008, 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 ?

  • Prakash February 16, 2009, 9:14 am

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

  • Kunchana August 13, 2009, 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

  • Luiz Rocha November 16, 2011, 9:09 pm

    Thanks a lot.

  • Brahma Reddy November 23, 2011, 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

  • Brahma Reddy November 23, 2011, 3:22 pm

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

  • Davide Ganz February 9, 2012, 1:20 pm

    Very Useful!
    Thanks for Sharing :)

Leave a Comment