One of Linux systems’ fantastic features is the ability to tweak and configure it to your liking. The command-line utility, commonly referred to as the Terminal, enables users to pass commands to the kernel and execute crucial tasks. It is one of the many features that bring out the difference between Linux and other operating systems.
In this post, we will exploit one of Linux’s powerful features: the ability to execute scripts/commands and applications automatically on startup. This process is done to achieve several things. It can range from fun and straightforward task such as changing the wallpaper to complicated stuff like backing up data or encrypting files.
Executing Linux Scripts on Startup
Let’s get started and look at the different methods we can use to automate Linux startup scripts and commands.
Method 1: Use ‘Startup Applications’ GUI App
Most of the methods that we will look at require the command-line. However, GNOME-based distributions have a simple graphical utility you can use. The ‘Startup Applications.’ It comes preinstalled and is a reliable tool for users who don’t like getting their hands dirty on the Terminal.
1. Launch ‘Startup Applications’ from the applications menu.
2. On the ‘Startup Applications’ main window, you will see three options on the right; Add, Remove, and Edit. To create our first automated script, click on the ‘Add’ button.
3. A pop-window will open. Enter the name of your startup script or program. In the command section, enter the command that you want to execute – say ‘sudo mkdir /home/tuts/Desktop/My-Test-Script.’ Alternatively, you can enter the full path to your bash script or executable system command in the various ‘bin‘ directories.
The above command will always give me a notification on my screen to update my system after every startup. That is just but a simple example. You can write advanced bash scripts to perform various tasks. If you are just getting started with bash-scripting, look at some of Bash For Loop examples here.
Method 2: Use Cron Job
Cron is a Linux daemon that executes user-edited tasks as specified by the user. These scheduled tasks are written in the crontab file. It tells the Cron which jobs to run at what time and under which events.
The basic format of a crontab file is:[minute] [hour] [DayOfMonth] [MonthOfYear] [DayOfWeek] [the script you want to execute].
However, for executing scripts on startup, we will use the @reboot parameter. If all these sounds new to you, please read our article about Crontab in Linux Explained With Examples.
To get started, add a new cron job, run the command below on the Terminal.
$ crontab -e
If you have never done this before, you will see an option to choose your default editor, as shown below. I would recommend nano since it’s much simple and straightforward. In my case here, I will enter ‘1.’
We will write a simple script that will create a new directory on the Desktop on startup.
SHELL=/bin/bash @reboot sleep 30 && DISPLAY=:0 My-Test-Directory
Method 3: Use the /rc.local
Another method to execute scripts and commands on startup is to use the /rc.local file. To get started, run the command below on the Terminal.
sudo nano /etc/rc.local
This command will open the rc.local file on the nano editor. If it were not present, it would create one. Enter your commands between the #! /bin/bash and exit 0 lines. You can also give the full path to your bash script here.
#! /bin/bash //path-to-your-bash-script exit 0
Now, we need to make the /rc.local file executable. Run the command below:
sudo chmod +x /etc/rc.local
Once done, reboot your PC and see the changes take effect.
Method 4: Systemd
Systemd is a software suite that contains various utilities to manage system services and OS processes. The systemd starts services and processes on boot.
We can use the Systemd to automate our scripts and commands on startup. Let’s create and automate a program to notify us to update the system using Systemd.
To get started, let’s the required folder and file where we will write our commands. Run the commands below:
$ mkdir -p ~/.config/systemd/user $ nano ~/.config/systemd/user/update-notifier.service
After the second command, the nano editor will open, paste the commands below here.
[unit] Description=Reminder to Update System PartOf=graphical-session-target [Service] ExecStart=bash -c 'sleep 10; notify-send "Please, Update your System Now" Type=oneshot [Install] WantedBy=graphical-session.target
These commands are pretty straightforward. The system will send the “Update-Reminder” 10 seconds after the graphical session has been loaded after startup.
Now to ensure the script is loaded after every startup, we need to make it executable. Run the commands below.
$ chmod 644 ~/.config/systemd/user/update-notifier.service $ systemctl --user enable update-notifier.service $ systemctl --user daemon-reload $ reboot
That is a simple way on how we can automate scripts with Systemd on startup. The above method executes scripts that don’t require root access. If you want to create a script that needs root access, create a systemd service in the “/etc/systemd/system” folder instead of “~/.config/systemd/user” directory. Also, you will need to omit the word ‘user’ included in the above command.
These are four methods you can use to Auto Execute Linux Startup Scripts and Commands on startup. If you are creating simple scripts that don’t require elevated permissions to execute, I recommend using the ‘Startup Application’ GUI app or a simple cron job. If your scripts need root access, then consider creating a Systemd service.