Linux : How to run a command when boots up?

Posted on in Categories News last updated July 24, 2005

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

13 comment

  1. > 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.

  2. 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 ?

  3. “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

  4. 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

    1. Add the following lines to your mystartup.sh file (at the top, under #!/bin/bash)
      ### BEGIN INIT INFO
      # Provides: scriptname
      # Required-Start: $remote_fs $syslog
      # Required-Stop: $remote_fs $syslog
      # Default-Start: 2 3 4 5
      # Default-Stop: 0 1 6
      # Short-Description: Start daemon at boot time
      # Description: Enable service provided by daemon.
      ### END INIT INFO

      Doc : https://wiki.debian.org/LSBInitScripts

Leave a Comment