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.

ADVERTISEMENTS

(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

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
13 comments… add one
  • Robin Jul 25, 2005 @ 16:10

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

    — Robin

  • Anonymous Aug 18, 2005 @ 14:19

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

  • Vivek Aug 18, 2005 @ 23:21

    > 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 Aug 19, 2005 @ 8:02

    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 Dec 18, 2008 @ 12:16

    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 Feb 16, 2009 @ 9:14

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

  • Kunchana Aug 13, 2009 @ 5:10

    “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 Nov 16, 2011 @ 21:09

    Thanks a lot.

  • Brahma Reddy Nov 23, 2011 @ 15:21

    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 Nov 23, 2011 @ 15:22

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

  • Davide Ganz Feb 9, 2012 @ 13:20

    Very Useful!
    Thanks for Sharing 🙂

  • What now? Feb 9, 2016 @ 0:17

    insserv: warning: script ‘mystartup.sh’ missing LSB tags and overrides

    • Python Feb 23, 2017 @ 15:59

      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 Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.