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).
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.
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.
Here is how to do this:
- Open a terminal.
- Open the crontab file:
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:
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.
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:
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.
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):
#!/bin/bash ### BEGIN INIT INFO # Provides: MyService # Required-Start: $all # Required-Stop: # Default-Start: 5 # Default-Stop: 6 # Short-Description: Your service description ### END INIT INFO /home/pat/test.sh
- 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).
Reminder: Remember that all the members of my community get access to this website without ads, exclusive courses and much more. You can become part of this community for as little as $5 per month & get all the benefits immediately.
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.
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.