How to run a command at boot for linux?

Execute a command at system startup

The first thing you need to do is create the script or program you would like to run on boot I will create a simple bash script for this example. The script will be called mystartup.sh and I will place it in /etc/init.d/ directory

# sudo vi /etc/init.d/mystartup.sh

now just ad some simple commands to this script

echo “Setting up customized environment…”

Then setup script permission :

#ls -l 

-rw-r--r-- 1 root root  53 Jun 21 12:18 myscript.sh

# chmod +x /etc/init.d/mystartup.sh

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

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

# update-rc.d mystartup.sh defaults 99
Note:

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.
mystartup.sh: 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 mystartup.sh. You can add more commands to this file or even call other shell/perl scripts from this file too.

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)

/root/fw.start

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.