start script on boot linux

4 Easy Ways To Run A Script At Startup On Linux

As a developer, I often create random scripts to complete various tasks on my computer and servers. Well, it looks like you might be the same, but starting them automatically, especially on boot, is not intuitive on Linux. So, let’s discuss a few ways you can easily do this.

Using the crontab to run a script on boot on Linux is the easiest method to remember. A script can be started automatically at startup, by adding a new line in the crontab, starting with the keyword @reboot and followed by the full command line.

I’ll give you more details about this method, but I’ll also explain 3 other alternatives (don’t miss the second one if you have a Desktop environment installed, as it’s effortless in this case).

Want the best experience? Become a premium member for ad-free browsing, access exclusive content, and ask questions in our private forums. Your membership helps support the site!
Linux doesn’t have to be intimidating. With my e-book, Master Linux Commands, you’ll uncover the secrets of the terminal in a fun, step-by-step journey. From basics to scripts, get ready to level up your Linux skills. Oh, and did I mention the handy cheat sheet you get as a bonus?

1 – Create a new cron task

Cron is a service that is automatically started at each boot of the computer and allows the user to execute scheduled commands.
A crontab is a file that will allow us to list what we want to start and when to start it, in a format the cron service understands.

Note: If you want to see all these steps in action, I have a video lesson available for the community members. You can join here and watch it directly if you are interested (with 20+ other lessons for Raspberry Pi and many other benefits).

The crontab has many options to start a script at a specific time or regularly (daily, weekly, 3 times a month, etc.).
By the way, I have a post about this specifically that you can check to learn more.
But that’s not what we are looking for today.

The crontab also adds the possibility to start a script on boot, with the @reboot option.
This is the first solution to start your program automatically, and probably the easiest one to remember in this list.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

Here is how to do this:

  • Open a terminal.
  • Open the crontab file:
    crontab -e
    If it’s your first time in the crontab, you need to select an editor (just press Enter to use Nano).
  • You get an empty crontab file, looking like this:
  • Create a new line starting with @reboot, and add your script command just after, like this:
    @reboot /home/pat/Desktop/
    It’s important to use the full path to your script in this command.
    The crontab doesn’t know where your script is located on the system drive.
  • Save and exit (CTRL+O, CTRL+X with nano).

Your task is now scheduled to start at each boot.

Note: If your task needs administrator privileges to run, just use “sudo” when opening the crontab. You’ll get access to another file, where you can put your command line, which will automatically use sudo on boot:
sudo crontab -e

I have a complete guide about Nano if you need a hand getting familiarized with it. It’s not that complicated, but you may need some guidance to get started (I also have one for Vim here, but I don’t use it or recommend it anymore).

2 – Use an application

On Ubuntu, and on most distributions with a desktop environment, there is a tool installed by default allowing you to run any script or application on startup. That’s the easiest method if you have GNOME installed.

I tested it with the default environment on Ubuntu, but I guess it should be similar to KDE, Mint, and alternatives.

Open your main menu, and find the “Startup Applications Preferences” icon in Lightpad:

When you click on it, you get a list of current programs configured to start automatically on boot.
And there is an “Add” button you can click on to add any program to this list:

Just fill the form with the name of your script, and the full command line to start it.
If I keep the command line from the previous method, I’ll put something like this in the “Command” field:

Click “Add” to save it, and you should have it on your list now.
You can easily switch it on or off without having to remove it from the list. So, it’s a very convenient tool.
Obviously, it only works if you have GNOME installed.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

I didn’t test with all environments, but I read that most of them have something similar. You may need to install the tool for some of them, but in most cases, you’ll find it directly in the main menu.

3 – Edit rc.local

Using the rc.local method is another method you can use to start a script on boot. It’s not complicated, but you have to remember the file name to know what to do.

The rc.local file is a script in itself, created for the system administrators (you!).
On boot, it’s one of the last things executed (at the end of the multi-user run level).
So, it’s exactly what you need.

Here is how to use it:

  • Open the rc.local file with nano:
    sudo nano /etc/rc.local
  • On Ubuntu, it looks like the file doesn’t exist by default, so you need to create the structure yourself.
    Paste the following lines in it:

    exit 0

    Some distributions (like Debian) might have it created by default.
    But the following step is the same anyway.
  • Then insert your script just before the “exit 0” line.
    Something like this:
  • Save and exit (CTRL+X).
  • If you created the file, make sure it has execution privileges with:
    sudo chmod +x /etc/rc.local

It’s all done, there’s nothing else to do.
You can reboot now to try, and your script will run automatically on boot.

Note: In this case, the script is run by root. No need to use sudo.

If you’re new to the Linux command line, this article will give you the most important Linux commands to know, plus a free downloadable cheat sheet to keep handy.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

4 – Create a new file in /etc/init.d

The last solution you can use is to create your script under /etc/init.d.
I generally prefer this method for something like a service (like a firewall script for example).

In best practice, there’s a minimal format to add in the header of your script. And it’s generally used for service (with start/stop/restart procedures), not only to run something on boot.

It works without it, but if you want to do it the right way, you should add it to your script too.
Obviously, it depends if you are doing this on a massive production server or on a Raspberry Pi at home. Do as you want.

Here is the full procedure to create a new file in this folder, that will start automatically on boot:

  • Create a new file in /etc/init.d:
    sudo nano /etc/init.d/myservice
    Change the file name to make it explicit.
  • Paste these lines (for example):
# Provides: MyService
# Required-Start:    $all
# Required-Stop: 
# Default-Start:     5 
# Default-Stop:      6 
# Short-Description: Your service description

  • The comments at the beginning describe when to start it during the boot process (runlevel 5 in this case). Change the service name and description to explain what your script is doing.
    But the important line is the last one, with the full path to your script.
  • Save your file and exit (CTRL+X).
  • Add the execution permission to this file:
    sudo chmod +x /etc/init.d/myservice
    By the way, don’t forget to do the same thing for your script, or it will never work.
  • Finally, you need to tell the system to start it on boot by using this command:
    sudo update-rc.d myservice defaults

That’s really the minimum to make it work.
The goal here is just to run a script on boot.

If you want to do this properly, you need to add at least a start, stop and restart options to your service.
You can find detailed documentation here if that’s what you want to do.

Reboot to check if it’s working. In theory, your script should run once every time you start your computer.

That’s it, you now have 4 ways to run your script at startup. It works with any programming language or even system command, just remember to use the full path for everything (including /usr/bin/php for example), and give execution privileges to your script (chmod +x).

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

Want to chat with other Raspberry Pi enthusiasts? Join the community, share your current projects and ask for help directly in the forums.

Additional Resources

Overwhelmed with Linux commands?
My e-book, “Master Linux Commands”, is your essential guide to mastering the terminal. Get practical tips, real-world examples, and a bonus cheat sheet to keep by your side.
Grab your copy now.

VIP Community
If you just want to hang out with me and other Linux fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.

Need help building something with Python?
Python is a great language to get started with programming on any Linux computer.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.

Similar Posts