How to run a command at boot for linux?

Execute a command at system startup

The first thing you need to do to run a command at boot for Linux is create a script or program you would like to run on boot.

Create a script

Let me take you threw the steps in how to  create a simple bash script for this example. The script will be called and I will place it in /etc/init.d/ directory but you can place it anywhere you want.

# sudo vi /etc/init.d/

now just add some simple commands to this script.

echo “This is my simple start up script running”

Save and close the file using !wq and then setup script permission :

# chmod +x /etc/init.d/

-rwxr-xr-x 1 root root53 Jun 21 12:18

Update rcrunlevel.d to run the command at boot

To make sure this script get executed every time we need to add it to the Linux system boot up:

# update-rc.d defaults 99

update-rc.d updates the System V style init script links /etc/rcrunlevel.d/name whose target is the script /etc/init.d/name. If any files named /etc/rcrunlevel.d/name already exist then update-rc.d does nothing. Your startup script name
defaults: The argument ‘defaults’ refers to the default runlevels, which are 2 through 5.
99: Number 99 means script will get executed before any script containing number 100. 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 You can add more commands to this file or even call other shell/perl scripts from this file too.

Execute shell script from system startup script

Your startup script can be edited to included other script that you would like to run as well. Keeping this to one file is a good idea to allow ease of cleanup and editing down the roadl.

Open the file in /etc/init.d/ directory (or your chosen directory)

# vi /etc/init.d/

Append your script path to the end as follows

/bin/bash /path/to/script

Save the file and you are done.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.