How to Auto Execute Linux Startup Scripts and Commands

Are you wondering how to autorun a script at startup? Here's are the best ways to auto-execute the Linux scripts and commands during the startup of your PC.

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.

Arun Kumar
Arun did his bachelor in computer engineering and loves enjoying his spare time writing for FOSS Linux. He uses Fedora as the daily driver and loves tinkering with interesting distros on VirtualBox. He works during the day and reads anything tech at night. Apart from blogging, he loves swimming and playing tennis.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,241FansLike
399FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

MUST READ

“I am because you are,” is the themed meaning behind the famed Ubuntu operating system. Moreover, this mindful phrase is practical because it continues to lure more individuals into the Ubuntu universe. Because great power beckons great responsibility, Ubuntu is stepping up. It realizes that different users will want to use the Ubuntu operating system software differently.
The Ubuntu Cinnamon Remix brings together Linux Mint's Cinnamon desktop with the Ubuntu Core. While some users are welcoming the new flavor of Ubuntu with open arms, others are scratching their heads, wondering where it fits in.

Manjaro vs. Ubuntu – which is better for you?

If you are a person associated with Computer technology and spend most of the time in the open-source arena, you must have heard or worked with some of the popular Linux distributions we have in the market. Some of the names that you will never miss are; Ubuntu, Arch Linux, Debian, and Mint.

Ubuntu Cinnamon Remix Review

The Ubuntu Cinnamon Remix brings together Linux Mint's Cinnamon desktop with the Ubuntu Core. While some users are welcoming the new flavor of Ubuntu with open arms, others are scratching their heads, wondering where it fits in.

Beaker Browser: A P2P web browser you must try

I think we can all agree on the fact that the web browser is an integral part of our Linux systems, or any computer system, for that matter. We have had several fulfilling options for browsing the web, like Firefox, Brave, Vivaldi, Tor, etc. and they have worked pretty well, but also pretty much in the same way, except for Tor, which is much better for anonymity.

Ubuntu 19.10 (Eoan Ermine) Beta Installation and Overview

It may be early, but I've been looking forward to the release of Ubuntu 19.10 for some time now. As an impatient person, and promised readers in the FOSSLinux article announcing the release of Eoan Ermine that we would provide you a review of Ubuntu 19.10 Eoan Ermine - Beta.  So, here it goes.