Home Learn Linux How to Auto Execute Linux Startup Scripts and Commands

How to Auto Execute Linux Startup Scripts and Commands

by Arun Kumar
AUTO-RUN PROGRAMS

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.

Launch Startup Applications

Launch Startup Applications

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.

Add a startup program

Add a startup program

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.

Update System

Update System

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.’

Select Editor

Select Editor

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
Reboot Cron Job

Reboot Cron Job

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
rc.local file

rc.local file

Now, we need to make the /rc.local file executable. Run the command below:

sudo chmod +x /etc/rc.local
rc.local file

rc.local file

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
Systemd file

Systemd file

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
Systemd File

Systemd File

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
Systemd file

Systemd file

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.

Conclusion

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.

You may also like

1 comment

Caltrop September 1, 2021 - 12:36 PM

-rwxr-xr-x 1 root root 41 Sep 1 12:19 /etc/rc.local

does not run in Mint

Reply

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.