Executing Commands and Scripts at Reboot & Startup in Linux

scripts at reboot

There might arise a need to execute a command or scripts at reboot or every time when we start our system. So how can we do that, in this tutorial we are going to discuss just that. We will discuss how we can make our CentOS/RHEL and Ubuntu systems to execute a command or scripts at reboot or at system startup using two different methods. Both the methods are tested and works just fine,

( Recommended Read : Use of du & df commands (with examples))

( Also Read : Files transfer using scp & rsync commands )


Method 1 – Using rc.local

In this method, we will use ‘rc.local’ file located in ‘/etc/’ to execute our scripts and commands at startup. We will make an entry to execute the script in the file & every time when our system starts, the script will be executed.

But we will first provide the permissions to make the file /etc/rc.local executable,

$ sudo chmod +x /etc/rc.local

Next we will add the script to be executed in the file,

$ sudo vi /etc/rc.local

& at the bottom of file, add the entry

sh /root/script.sh &

Now save the file & exit. Similarly we can execute a command using rc.local file but we need to make sure that we mention the full path of the command. To locate the full command path, run

$ which command

For example,

$ which shutter


For CentOS, we use file ‘/etc/rc.d/rc.local’ instead of ‘/etc/rc.local’. We also need to make this file executable before adding any script or command to the file.

Note:- When executing a script at startup, make sure that the script ends with ‘exit 0’.


Method 2 – Crontab method

This method is the easiest method of the two methods. We will create a cron job that will wait for 90 seconds after system startup & then will execute the command or script on the system.

To create a cron job, open terminal & run

$ crontab -e

& enter the following line ,

@reboot ( sleep 90 ; sh /location/script.sh )

where /location/script.sh is the location of script to be executed.

So this was our tutorial on how to execute a script or a command when system starts up. Please leave your queries, if any , using the comment box below.

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Become a Supporter – Donate us some of you hard earned money:

Linux TechLab is thankful for your continued support.


Passionate about Linux & open source. Loves to learn, read & write about Linux as well as new technologies.

You may also like...

6 Responses

  1. Piet says:

    Hello, are the slahes and backslashes correct?
    @reboot ( sleep 90 ; sh \location\script.sh )
    @reboot ( sleep 90 ; sh /location/script.sh )

  2. HarryG says:

    Hi, Thanks for this helpful information.
    I’ve installed TightVNC on my (headless) file server which is running Ubuntu 18.04 LTS 64bit.
    I start the vnc server with,
    $ vncserver -geometry 1280×720 -depth 24
    New ‘X’ desktop is krt1293:1

    Starting applications specified in /home/bob/.vnc/xstartup
    Log file is /home/bob/.vnc/krt1293:1.log

    I’m accessing it from my main PC (Xubuntu 16.04 LTS 64bit) using Reminna.
    I run,

    $ ssh -L 5901:localhost:5901 -N -f -l USER IP

    I then am able to connect using Remmina with settings,
    Protocol: VNC – Virtual Network Computing
    Server: localhost:5901

    The connection is established and all is working OK to this point.
    I then decided to setup so vncserver is started at bootup using a crontab.

    Opened user crontab file with,
    $ crontab -e
    Added the following line at the bottom and saved,
    @reboot sleep 90 ; sh /location/script.sh
    Rebooted – cron failed to start vncserver
    Edited the following line at the bottom and saved,
    @reboot sleep 90 ; sh /location/script.sh

    Rebooted – cron failed to start vncserver
    Edited the following line at the bottom and saved,
    @reboot (vncserver -geometry 1280×720 -depth 24 :1)
    Rebooted – cron failed to start vncserver
    Edited the following line at the bottom and saved,
    @reboot ( vncserver -geometry 1280×720 -depth 24 :1 )
    Rebooted – cron failed to start vncserver
    I added a new job to the crontab to confirm that the crontab is being executed at startup,
    @reboot ( touch /home/bob/tmp/cron_test )

    The “vncserver” crontab failed, but the “touch” crontab was successful. Any thoughts on what I’m doing wrong?

  3. Jorge Garcia says:

    I have to activate my wifi hardware each time that I reboot my system with this command ’sudo modprobe -v b43’ and I am looking to make this task easier, do you think that I can give sudo privileges?

    thanks for your time and help

    • Shusain says:

      A quick Google search with “Persistent Module Loading” as search terms will give you the solution you need.
      Also i will be posting an article for the same in coming weeks.

Leave a Reply

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