How to Auto Execute Commands/Scripts During Reboot or Startup

I am always fascinated by the things going on behind the scenes when I boot a Linux system and log on. By pressing the power button on a bare metal or starting a virtual machine, you put in motion a series of events that lead to a fully-functional system – sometimes in less than a minute. The same is true when you log off and / or shutdown the system.

What makes this more interesting and fun is the fact that you can have the operating system execute certain actions when it boots and when you logon or logout.

In this distro-agnostic article we will discuss the traditional methods for accomplishing these goals in Linux.

Note: We will assume the use of Bash as main shell for logon and logout events. If you happen to use a different one, some of these methods may or may not work. If in doubt, refer to the documentation of your shell.

Executing Linux Scripts During Reboot or Startup

There are two traditional methods to execute a command or run scripts during startup:

Method #1 – Use a cron Job

Besides the usual format (minute / hour / day of month / month / day of week) that is widely used to indicate a schedule, cron scheduler also allows the use of @reboot. This directive, followed by the absolute path to the script, will cause it to run when the machine boots.

However, there are two caveats to this approach:

  1. a) the cron daemon must be running (which is the case under normal circumstances), and
  2. b) the script or the crontab file must include the environment variables (if any) that will be needed (refer to this StackOverflow thread for more details).

Method #2 – Use /etc/rc.d/rc.local

This method is valid even for systemd-based distributions. In order for this method to work, you must grant execute permissions to /etc/rc.d/rc.local as follows:

# chmod +x /etc/rc.d/rc.local

and add your script at the bottom of the file.

The following image shows how to run two sample scripts (/home/gacanepa/script1.sh and /home/gacanepa/script2.sh) using a cron job and rc.local, respectively, and their respective results.

script1.sh:
#!/bin/bash
DATE=$(date +'%F %H:%M:%S')
DIR=/home/gacanepa
echo "Current date and time: $DATE" > $DIR/file1.txt
script2.sh:
#!/bin/bash
SITE="Tecmint.com"
DIR=/home/gacanepa
echo "$SITE rocks... add us to your bookmarks." > $DIR/file2.txt
Run Linux Scripts at Startup

Run Linux Scripts at Startup

Keep in mind that both scripts must be granted execute permissions previously:

$ chmod +x /home/gacanepa/script1.sh
$ chmod +x /home/gacanepa/script2.sh

Executing Linux Scripts at Logon and Logout

To execute a script at logon or logout, use ~.bash_profile and ~.bash_logout, respectively. Most likely, you will need to create the latter file manually. Just drop a line invoking your script at the bottom of each file in the same fashion as before and you are ready to go.

Summary

In this article we have explained how to run script at reboot, logon, and logout. If you can think of other methods we could have included here, feel free to use the comment form below to point them out. We look forward to hearing from you!

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Gabriel Cánepa

Gabriel Cánepa is a GNU/Linux sysadmin and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

10 Responses

  1. Liz Thompson says:

    Linux mint has no rc.d folder, it does have rc0.d, rc1.d up to rc6.d/etc/rc.local does not execute in Linux Mint but is there.

    There is an /etc/init.d/rc.local that supposedly executes /etc/rc.local but doesn’t.

    • Liz Thompson says:

      I fixed things…

      /etc/profile can me made executable and allows system wide settings to be made here.

      Better yet I wrote a script ‘startup‘ and put in my local ‘bin‘ directory and added it to Mint Mate’s startup programs.

      Now I can edit without root permission and set all kinds of things.

      Things got crazy when I added the new external monitor HDMI, touchscreen, sound and all needed tweaking.

  2. dbp says:

    rc.local is obsolete, use systemd instead

  3. Dan St.André says:

    I seem to recall that one can run a script as a side effect of the ‘shutdown’ command.

  4. WladyX says:

    What’s the font used in the screenshot?
    Thanks

  5. Shane Faulkinbury says:

    What if I don’t have an rc.d folder, I just have rc0.d through rc6.d folders.

  6. Karim says:

    Hi, thanks for that tutorial. I tried method #2 with an iptables script, which should execute after a reboot. Unfortunately it doesnt work, do you know why? Is it possible that the iptables service is not yet started when the script tries to execute the rules?

Got something to say? Join the discussion.

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.