Linux's systemd service configuration and automatic restart


layout: POST
title: Linux's systemd service configuration and automatic restart
DATE: 2019-09-09
Tags: Linux
---

Linux's systemd service configuration and automatic restart

0 background

When developing on linux, often need to make their program services, and implement the service to start automatically restart, and automatic restart function After the service crashes, we do a brief introduction to the function of the realization, implementation is very simple to use linux system the systemd can be realized

1 systemd Introduction

Historically, linux startup has been using the init process, such as

$ sudo /etc/init.d/apache2 start

or

$ service apache2 start

This approach has two drawbacks.

First, the long start-up time. The init process is the serial started, only the first to start a process completed, will start the next process.
Second, the startup script complicated. init process just execute the startup script, regardless of other things. The script needs its own deal with various situations, which often makes the script becomes very long.

Systemd to solve these problems and birth. Its design goal is to provide a complete solution for start-up and management of the system.

According to Linux convention, letter d is an abbreviation daemon (daemon) is. Systemd meaning of the name, it is to be the guardian of the entire system. Use the Systemd, then you do not need the init. Systemd replaced initd, became the first system of process (PID = 1), other processes are its children.

systemctl Systemd main command for system management. For users, the most commonly used is the following commands to start and stop the Unit (main service).

- Immediately start a service

$ systemctl start apache.service

- Stop a Service

$ systemctl stop apache.service

- restart a service

$ systemctl restart apache.service

- a service kill all child processes

$ systemctl kill apache.service

- reload a service profile

$ systemctl reload apache.service

- reload all modified configuration files

$ systemctl daemon-reload

- display all the underlying parameters of a Unit

$ systemctl show httpd.service

- displays the value of the specified property of a Unit

$ systemctl show -p CPUShares httpd.service

- Sets the specified property of a Unit

$ systemctl set-property httpd.service CPUShares=500

This article is the use of systemd are introduced, if you want to learn more about the basics of systemd can access to relevant information

2 server-side scripting

Here we write a php script service server.php, used to implement a service

<?php
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($sock, '0.0.0.0', 10000);
for (;;) {
    socket_recvfrom($sock, $message, 1024, 0, $ip, $port);
    $reply = str_rot13($message);
    socket_sendto($sock, $reply, strlen($reply), 0, $ip, $port);
}

After running lsof command can be used to view the port occupancy

lthpc@lthpc:~$ lsof -i:10000
COMMAND   PID  USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
php     40446 lthpc    3u  IPv4 37381218      0t0  UDP *:10000 

Instruction simulation using nc client test

$ nc -u 127.0.0.1 10000
Hello, world!
Uryyb, jbeyq!

3 Create service
next use systemd to create a service, write a service profile/etc/systemd/system/rot13.service

[Unit]
Description=ROT13 demo service
After=network.target
StartLimitIntervalSec=0
 
[Service]
Type=simple
Restart=always
RestartSec=1
User=ltpc
ExecStart=/usr/bin/env php /path/to/server.php
 
[Install]
WantedBy=multi-user.target

There are several points to note, in order to allow the service to automatically restart unlimited times, need to add the following configuration

StartLimitIntervalSec=0

Restart=always

RestartSec=1

The specific parameters of the meaning of the configuration file, refer to the document

After setting up, you can run the following statement to start the service

$ systemctl start rot13

After the run, he launched a service called rot13, you can use the status view service status

lthpc@lthpc:~/workspace_zong/tcptest$ systemctl status rot13
● rot13.service - ROT13 demo service
   Loaded: loaded (/etc/systemd/system/rot13.service; disabled; vendor preset: enabled)
   Active: active (running) since 一 2019-10-28 11:25:43 CST; 1min 28s ago
 Main PID: 44532 (php)
    Tasks: 1
   Memory: 5.2M
      CPU: 24ms
   CGroup: /system.slice/rot13.service
           └─44532 php /home/lthpc/workspace_zong/tcptest/server.php
 
10月 28 11:25:43 lthpc systemd[1]: Started ROT13 demo service.

To boot automatically start, execute the following statement

$ systemctl enable rot13

Similarly, you can use the nc instruction simulated client testing, we can see that the service has started normal operation!

4 automatic restart
To test whether you can automatically restart normal, we manually kill the service process is started, and then view the process ID has been found to replace the PID number, indicating the restart process too, and use nc -u 127.0.0.1 10000 instruction tests can still call service

$ systemctl status rot13 | grep PID
 Main PID: 44532 (php)
$ sudo kill 44532
$ systemctl status rot13 | grep PID
 Main PID: 44255 (php)

Note input systemctl stop rot13when the service is not restarted, so if there is need to modify the parameters, run directly stop command to change completely and then start it


Writing the Service Configuration

vim /lib/systemd/system/website.service

[Unit]
Description=website
After=network.target
 
[Service]
Type=forking
ExecStart=/home/monitor/website/start.sh
ExecReload=/home/monitor/website/restart.sh
ExecStop=/home/monitor/website/shutdown.sh
 
[Install]
WantedBy=multi-user.target

Write the corresponding start and stop scripting
on:

vim /home/monitor/website/start.sh
#!/bin/sh
export JAVA_HOME=/usr/java/jdk1.8.0_144
export PATH=$JAVA_HOME/bin:$PATH
 
nohup java -jar /home/monitor/website/demo-0.0.1-SNAPSHOT.jar &

shut down:

vim /home/monitor/website/shutdown.sh
#!/bin/sh
ps -ef | grep demo-0.0.1-SNAPSHOT.jar | grep -v grep | awk '{print $2}' | xargs kill -9

Restart:

vim /home/monitor/website/restart.sh
#!/bin/sh
export JAVA_HOME=/usr/java/jdk1.8.0_144
export PATH=$JAVA_HOME/bin:$PATH
 
ps -ef | grep demo-0.0.1-SNAPSHOT.jar | grep -v grep | awk '{print $2}' | xargs kill -9
nohup java -jar /home/monitor/website/demo-0.0.1-SNAPSHOT.jar &

Authorized to run:

chmod +x start.sh shutdown.sh restart.sh

Configuring since the launch of
the boot:systemctl enable website

start up:systemctl start website

shut down:systemctl stop website

Restart:systemctl restart website

View status:systemctl status website

Modify service configuration revalidated:systemctl daemon-reload

Common systemctl error code
| code | desc |
|: ------ |: ------------------------------- -------------------------- |
| 0 | The command completed successfully |
| 1 | General unknown error |
| 2 | misuse shell commands |
| 126 | command is not executed |
| 127 | could not find command |
| 128 | invalid exit parameters |
| 128 + x | a serious mistake Linux signal x |
| 130 | Linux signal serious errors 2, that command SIGINT (Ctrl + C) termination |
| 203 | missing identifies script execution |
| 255 | exit status of cross-border |

Remarks
such as running sh script reported the following error:
/ bin / sh ^ M: bad interpreter: No such file or directory
is due to script files copied to linux editor in the windows caused the end of each line to edit the text under the windows are \ n \ r, and the Linux is \ n,
the solution:
the input terminal -i Sed 'S / \ R & lt // $' daemon.sh
Sed -i 'S / \ R & lt // $' will daemon.sh the end of the row of make-all-linux-project.sh \ r replaced with a blank, wherein the error daemon.sh script.
To avoid this error, you can create a new script directly in linux or linux environment and then copy.

Guess you like

Origin www.cnblogs.com/nxzblogs/p/11755972.html