SoFunction
Updated on 2024-11-10

Explanation of the process of deploying services using systemd

1. Preamble

Usually, we inevitably need to deploy our services during development, but how should we go about it? If the host is rebooted, how can the service start by itself? Maybe you already have a few different answers in mind, so let's take a look at them next.

2. How to deploy services

Suppose, we now have a python project (named xx) and now we want to deploy it on a Linux server, how should we do it?

Content of xx

#!/usr/bin/python
import time
i=0
while True:
    print i
    i+=1
    (60)

2.1. Background process mode startup

In the first way, we can start this project directly as a background process.

nohup python xx &

2.2、Use the systemd way to start up

In the second way, we can host the project through systemd.

About systemd: systemd is a piece of software for the Linux platform that supports starting tasks as daemons, cgroup resource isolation, and can very efficiently load our services during the Linux boot process.

2.2.1 Creating a service file

The path where the xx file is located:/root/

The path to the service file:/etc/systemd/system/

element

[Unit]
Description=myself service
[Service]
ExecStart=/root/xx
[Install]
WantedBy=

Since we defined the interpreter in the xx file, we can just add executable permissions to xx and specify the path to xx directly in the startup command.

2.2.2. Starting services

Specifies the command for the service file to enable the xx service:

systemctl -f enable /etc/systemd/system/

Start the xx service:

systemctl start xx

Check the status of the xx service:

systemctl status xx

3、How to start automatically

3.1. Automatic start-up of documents

3.1.1、

For services deployed by background processes, we can add the startup command to the bootstrap file.

Autostart file path:/etc/

3.1.2、

In addition to adding the startup command directly to the file, we can also move the executable script directly to the/etc///Path down.

3.2、systemd

systemd will start our service by default when the server starts because we have enabled the service. However, to prevent the service from being unrecoverable if it exits abnormally, we can add to the service file the[Service]Configuration block to addRestart=on-failureconfiguration to automatically restart the service when it exits abnormally.

to this article on the use of systemd deployment services process analysis of the article is introduced to this, more related to the deployment of systemd service content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!