Methods of payment Abuse

How to add a script to Ubuntu autoloader

24.02.2021, 20:29

In some cases, it is necessary to execute your script when the system boots up. Such cases include the need to change the screen resolution, launch certain applications, and update a particular utility. Running a script can be accomplished in a variety of ways. You can do this by using a graphical shell or the systemd initialization system, which is now used in almost all distributions.

Autoloading by using the standard Ubuntu utility

The first thing to do is to create a script in a convenient location and execute it:

$ sudo gedit /script_path/script_name.sh

#!/bin/bash

echo "Hello world"

This script will display "Hello world" messages to the user. Once the program is ready, it needs to be made executable. To do this, the following command is used:

$ sudo chmod ugo+x /script_path/script_name.sh

Next, in the main menu of the system, you need to launch the "Automatically Run Applications" utility.

Автоматически запускаемые приложения

Click the "Add" button and enter the full path to the script file in the "Command" field. You can press the "Browse" button, find and select the script file and press "Add".

Графическая оболочка Ubuntu

In the future, the program will be executed every time you start the Ubuntu graphical shell.

Autoload Linux scripts in Systemd

Use a special command to create a systemd service file

This is the command:

sudo systemctl edit --force myscript

You need to add the contents to this file:

[Unit]

Description=My Script Service

After=multi-user.target

[Service]

Type=idle

ExecStart=/full/path/to/script/script_name.sh

[Install]

WantedBy=multi-user.target

Создание файла сервиса systemd

Next, in the line called ExecStart you need to fulfill one of the following requirements: write the path to the script or command to be executed. After that the script is added to the autoloader:

$ sudo systemctl daemon-reload

$ sudo systemctl enable mysrcipt

The program will start after system initialization. For this purpose, you can use the "old" method: you can use rc.local to create the file /etc/rc.local and write the path to it in the ExecStart line of the service file.