Configure Application or Script to Start on Boot in Linux

Configure Application or Script to Start on Boot in Linux

Introduction

Configuring applications or scripts to start automatically on boot in a Linux system is a common requirement. Whether for critical services in a server environment or for frequently used tools on a personal computer, proper configuration can achieve automated startup, enhancing system availability and convenience. This article will detail how to use Systemd to configure applications or scripts to start on boot in Linux.

Using Systemd Service Method

Systemd is the default initialization system and service manager for most Linux distributions (such as Ubuntu, CentOS, etc.). By creating a Systemd service unit file, you can easily achieve the goal of starting applications or scripts on boot.

Service unit files can be stored in two locations: system-level and user-level:

System-Level:

Located in the /etc/systemd/system/ directory, these service units are aimed at the entire system and are automatically started at boot based on the run level, providing basic services for all users.

User-Level:

Stored in the ~/.config/systemd/user/ directory, these are specific to user sessions and start upon user login, used for managing personal tasks and applications.

Taking the system-level service as an example, we will create a new service unit file in the /etc/systemd/system/ directory. Using a text editor (such as nano or vim), create a file ending with .service, such as nginx.service. Below is a simple service unit file example for starting an application named nginx:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target

[Service]
ExecStart=/usr/sbin/nginx
ExecStop=/usr/sbin/nginx -s stop
Restart=on-failure

[Install]
WantedBy=multi-user.target
  • [Unit] Section:

    • Description: A brief description of the service unit, such as: NGINX HTTP and reverse proxy server.

    • After: Specifies when the service unit should start, in this case, indicating that nginx should start after the network is up.

  • [Service] Section:

    • ExecStart: Defines the command to execute when starting the service, here it is /usr/sbin/nginx.

    • ExecStop: Defines the command to execute when stopping the service, here it is /usr/sbin/nginx -s stop.

    • Restart: Controls whether the service should automatically restart under certain conditions. Restart=on-failure means that if the nginx service exits due to an unexpected error (such as a crash or non-zero exit status), Systemd will automatically try to restart the service.

  • [Install] Section:

    • WantedBy: Specifies the targets under which the service unit should be started automatically. WantedBy=multi-user.target indicates that the nginx service will start automatically when the multi-user.target is enabled. multi-user.target is a common target in Systemd, usually corresponding to a multi-user graphical or command-line environment, thus the nginx service will automatically start with the system in most common multi-user run levels.

After creating or modifying the service unit file, you need to reload the Systemd configuration to make Systemd read the new file by executing:

sudo systemctl daemon-reload

Next, enable the service to achieve automatic startup:

sudo systemctl enable nginx.service

This operation will create a symbolic link, linking the service unit file to the corresponding Systemd target directory, thus achieving the goal of starting on boot.

If you need to stop the service, you can use the following command:

sudo systemctl stop nginx.service

When the application or script is updated, you need to restart the service to apply the changes by executing:

sudo systemctl restart nginx.service

If you no longer need the service to start on boot, you can disable it using the following command:

sudo systemctl disable nginx.service

This will remove the previously created symbolic link, and the service will no longer start automatically on boot.

Welcome to follow my public account for more quality content!

Leave a Comment