How to Run Linux Commands and Scripts Automatically on a System Reboot

Chưa phân loại
Running apps and scripts automatically on startup can be useful for automating common boot tasks and events. This article will explain a few methods that can be used for launching apps and scripts on a fresh reboot or a new login.

Startup Applications

Ubuntu and other GNOME based distributions come with an application simply called “Startup Applications”. It can be used for managing apps and scripts that run on a fresh system reboot or login.

Launch the “Startup Applications” app from the application launcher and click on “Add” button to add a new entry.

Fill up the “Name” and “Command” fields as per your needs and then click on “Add” button to finish creating a new entry. The entry created in the screenshot below will send a “Make a Backup” reminder as a system notification on every reboot / login. You can replace it with your own command or with full path of your bash script. You can also use any existing system command or executables usually located in various “bin” folders across the filesystem.

As stated above, a backup reminder is shown on every reboot.


Systemd is a daemon and service manager that contains various utilities to manage system processes and OS components. In its simplistic form, it is typically used to start and end services in a fresh boot cycle.

Systemd can be used to automatically launch an app or run a script on new a boot. To create the same backup reminder notification explained above, first you have to create the required folders and file by running the commands below:

$ mkdir -p ~/.config/systemd/user
$ nano ~/.config/systemd/user/backup_reminder.service

Replace “nano” with command of your favorite text editor. Replace “backup_reminder” with any other name that you prefer.

Paste the code below in the backup_reminder.service file created using the command above.

Description=Sends a backup reminder on every reboot

ExecStart=bash -c ‘sleep 10; notify-send "Make a Backup"’


The code above is pretty straightforward. It sends a “Make a Backup” notification 10 seconds after the graphical session is loaded (once every reboot or login).

Run the commands below to enable the service so that it can automatically run on every reboot.

$ chmod 644 ~/.config/systemd/user/backup_reminder.service
$ systemctl –user enable backup_reminder.service
$ systemctl –user daemon-reload
$ reboot

This is but a simple example of running a basic command on boot using systemd. You can also create advanced services with multiple conditions and multiple commands. For more information, refer to systemd man page by running the command below:

$ man systemd

Note that this example explains creating a new service that doesn’t require root access and is suitable for auto-starting apps that doesn’t require root permissions. If you want to auto-start scripts requiring root access, you have to create a new systemd service in “/etc/systemd/system” directory instead of “~/.config/systemd/user” folder and omit “–user” switch in the commands mentioned above.

Cron Job

Cron is a tool that can periodically run scheduled tasks according the conditions specified by a user. These scheduled jobs are created in Crontab in a pre-defined format. In simple terms, Crontab tells Cron which jobs to run at what point of time.

Like systemd, crontab jobs can be used to launch apps and run scripts automatically on boot. To add a new cron job, run the command below:

$ crontab -e

Add the following lines at the end of text file (automatically launches GNOME terminal on every reboot):

@reboot sleep 30 && DISPLAY=:0 gnome-terminal

You can use your own command or supply full path to a shell script.

Note that unlike systemd, cron can’t detect if graphical session has been loaded or not. You have to specify some estimated wait period till the X server loads and a display identifier. You can know about your display ID by running the command below:

$ echo $DISPLAY

The delay before execution of command or script depends on your system configuration and boot time.


Another method to run scripts and commands at startup is to use “rc.local” file. Note that in my testing, I was not able to defer script execution till graphical session was live. Adding any sleep delay lead to delay in the showing of the login screen itself. Because of this, I had no success in running graphical apps on startup using rc.local file. Editing rc.local also requires root access, unlike all other examples explained above.

To add commands / scripts to rc.local file, run the command below (creates a new rc.local file if it doesn’t exist):

$ sudo nano /etc/rc.local

Add your commands between “#! /bin/bash” and “exit 0” lines, as shown below:

#! /bin/bash
exit 0

Make rc.local file executable by running the command below:

$ sudo chmod +x /etc/rc.local

Just reboot for the startup script to take effect.


These are a few methods that can be used to automatically run scripts and apps on startup. If you are looking to run scripts that doesn’t require root access, I would recommend using “Startup Applications” GUI app. If you want to run apps and scripts with root access, I would suggest you to create a system level systemd service.

Sandclock IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, Sandclock IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Make a bootable Linux USB using Linux Live USB Creator 2.9

Linux Live USB Creator is a free yet powerful open-source software for creating a portable, bootable and virtualized USB...

How to compare strings in Bash

For different programming purposes, we need to compare the value of two strings. Built-in functions are used in many programming...

20 Best Ubuntu Applications for 2018

20 Best Ubuntu Applications Recommended for 2018 The popularity and user base of Linux is increasing rapidly hence many...
Bài Viết

Bài Viết Mới Cập Nhật

Hướng dẫn chuyển đổi windows server windows evaluation to standard và active windows server 2008 + 2012 + 2016 + 2019

How to Update Ubuntu Linux

Squid Proxy Manager cài đặt và quản lý Proxy Squid tự động trên ubuntu

Hướng dẫn cài đặt Apache CloudStack

Hướng dẫn ký file PDF bằng chữ ký số (chữ ký điện tử) và sửa lỗi mới nhất 2021 foxit reader