SoFunction
Updated on 2025-03-03

Tutorial guide for setting up service service on Linux

Preface

In Linux systems, ensuring that critical services can run automatically when the system starts is a very important task. Especially in a server environment, we hope that some services (such as databases, application services, or custom scripts) can be automatically started after each startup of the system, thereby ensuring the continuous operation of the business. In Linux, there are two commonly used service management systems: systemd and sysvinit. This guide will provide detailed instructions on how to set up the service's boot in different init systems, and explain how to create and configure a custom systemd service.

1. Overview of Linux service management

In Linux, services are processes running in the background that starts running when the system starts and stops when the system is shut down. The tools that manage these services vary by operating system version:

  1. systemd: Most modern Linux distributions (such as CentOS 7+, Ubuntu 16.04+, Debian 8+) are usedsystemdTo manage system services.
  2. sysvinit: Used by some older Linux distributions (such as CentOS 6 or Ubuntu 14.04)sysvinitCarry services.

Each service management tool has its own configuration method and management commands. In modern Linux systems,systemdis the most common service management tool, so it is also the focus of our discussion.

2. Set up the service to start and start it up in systemd

2.1 Systemd Introduction

systemdIt is a system and service manager of Linux. It is responsible for the system boot process, manages the system's runtime services, and provides many system functions, such as logs, time synchronization, etc. It passes through the service unit file (usually.serviceFile) to define and manage services, and provide powerful dependency management and parallel startup functions.

2.2 How to check the status of the service

First, you need to make sure that the service has beensystemdManage and can be started manually. You can check the status of the service using the following command:

sudo systemctl status <service_name>

For example, if your service name isnginx, you can check its status using the following command:

sudo systemctl status nginx

The output information will show whether the service is running, as well as the service's log and status information. If the service has beensystemdManagement, it displays the details of the current service.

2.3 Enable the service to start and start it up

To set a service to run automatically when the system starts, you can usesystemctl enableOrder. It adds the service to the system's startup item, ensuring that the service is started every time it starts.

sudo systemctl enable <service_name>

For example, to set upnginxThe service starts automatically and runs the following commands:

sudo systemctl enable nginx

This will be/etc/systemd/system//Create a symbolic link in the directory for the specified service to ensure that the service runs automatically when the system starts.

2.4 Manually start and stop service

If you want to start or stop a service immediately, you can use the following command:

Start the service

sudo systemctl start <service_name>

Stop service

sudo systemctl stop <service_name>

These commands will immediately start or stop the specified service. You can use them to verify that the service is functioning properly.

2.5 Check whether the service is successfully enabled

You can use the following command to check whether the service has been successfully enabled for booting:

sudo systemctl is-enabled <service_name>

If the service is successfully enabled, the output will displayenabled. If the service is not enabled or is not configured correctly, the output will displaydisabled

2.6 Disable the power-on self-start service

If you no longer want a service to start automatically when the system starts, you can usesystemctl disableOrder:

sudo systemctl disable <service_name>

For example, disablenginxThe command to start the service is:

sudo systemctl disable nginx

After disabling the service, it will no longer start automatically when the system starts, but you can still start the service manually.

3. Set up service startup in sysvinit

Although systemd is the standard for current Linux distributions, older distributions (such as CentOS 6, Debian 7, etc.) still use sysvinit to manage services. In sysvinit, the service's power-on configuration is different from systemd, and the following are the relevant steps.

3.1 Use chkconfig to set up power-on self-start

chkconfigIt's managementsysvinitA tool for self-starting service. You can use it to enable or disable the service's boot.

  • Enable Power-on Self-Start
sudo chkconfig <service_name> on
  • Disable booting
sudo chkconfig <service_name> off

For example, to enablehttpdThe (Apache) service starts automatically and runs the following command:

sudo chkconfig httpd on

3.2 Manually manage startup scripts

existsysvinitIn the system, the service startup script is stored in/etc//In the directory. You can manage the startup of the service by manually adding the startup script of the service.

List all services

You can view all available service scripts on the system using the following command:

ls /etc//

Manually add service to boot

You can useCommand adds the service to the startup item:

sudo  <service_name> defaults

This adds a service at the appropriate run level, ensuring it runs automatically when the system starts.

4. Create a custom systemd service

If you have a custom script or program that you want to run when the system is started, you can do so by creating a systemd service.

4.1 Create a service unit file

The systemd service is managed through a service unit file (.service file). First, you need to create a new service file in the /etc/systemd/system/ directory. For example, create a file named :

sudo nano /etc/systemd/system/

4.2 Service unit file configuration

Add the service-related configuration in the file. Here is an example of a custom service:

[Unit]
Description=My Custom Service
After=

[Service]
ExecStart=/path/to/your/program --argument
Restart=always
User=youruser
Group=yourgroup

[Install]
WantedBy=

explain:

  • [Unit]: Define the description and dependencies of the service.After=It means that the service will be started after the network is started.
  • [Service]: Define the service startup command, restart policy, run user, etc.ExecStartis a command executed when the service starts.
  • [Install]: Defines the level at which the service is started.Indicates that the service will be started in multi-user mode.

4.3 Reload systemd configuration

After saving the service unit file, reload it using the following commandsystemdConfiguration:

sudo systemctl daemon-reload

This will tellsystemdRead the new service unit file and update the service list.

4.4 Enable and start the service

Next, you can enable and start the service:

sudo systemctl enable myservice
sudo systemctl start myservice

4.5 Check service status

Use the following command to check if the custom service is running:

sudo systemctl status myservice

The output information will show the current status of the service, the log, and whether it is successfully started.

5. Frequently Asked Questions and Troubleshooting

You may encounter some common problems when setting up the service to power on and start it up. Here are some common problems to troubleshoot.

5.1 The service fails to start or fails to start

If you find that the service cannot be started, first check the status of the service:

sudo systemctl status <service_name>

If the service starts up, the output information will usually show the reason for the failure. You can further view detailed logs to help diagnose problems:

sudo journalctl -xe

5.2 The service is not enabled and the startup is enabled.

Check whether the service is enabled and enabled:

sudo systemctl is-enabled <service_name>

If displayeddisabled, you can re-enable the power-on self-start:

sudo systemctl enable <service_name>

5.3 Systemd Service Unit File Configuration Error

If the custom service you created does not work properly, check the following points:

  • **Path and command

Is it correct**: Make sureExecStartThe path and command specified in   is correct.

  • Permissions issues: Ensure that the service's running user has permission to execute the commands and access files required by the service.
  • Log check:usejournalctlCommand to view the service log and find detailed error information.

6. Summary

Setting up service startup in Linux systems is one of the important tasks to ensure the stable operation of the server. Whether using systemd or sysvinit, you can easily manage the startup behavior of your system services. Most modern Linux systems use systemd to manage services, which provides flexible service management functions and rich logging functions. In older Linux distributions, the sysvinit system also has a simple service management method.

Additionally, creating custom services and configuring them to boot is a common requirement in Linux management. By writing custom .service files and managing them with systemd, you can easily control the up and running of custom services.

I hope this article can help you correctly set up and manage the boot of the service in Linux system. If you encounter problems in actual operation, it is recommended to troubleshoot according to the system log and service status and adjust the configuration in time.

The above is the detailed content of the tutorial guide for setting up the Linux Service Service to start up automatically. For more information about the Linux Service to start up automatically, please pay attention to my other related articles!