systemd adds custom system service Settings to customize startup methods

  • 2020-05-15 03:18:50
  • OfStack

1. Service permission

systemd has a distinction between systems and users; System (/ user/lib/systemd system /), user (/ etc/lib/systemd user /). 1 as the system administrator manually create unit file Suggestions in/etc systemd system/directories below.

2. Create a service file


[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

[Unit]

Description: a simple description of the service

Documentation: service documentation

Before, After: define the startup sequence. Before= xxx.service, which means that the service is started before xxx.service is started. After= xxx.service, which means that the service is started after xxx.service.

Requires: this unit is started, and the units it needs will be started as well. The unit it needs is stopped, and so is the unit.

Wants: recommended. This unit starts, and the units it needs will be started; The unit it needs is stopped and has no effect on the unit.

[Service]

Type=simple (default) : systemd considers that the service will start immediately. The service process will not fork. If the service starts another service, do not start with this type unless the service is socket activated.

Type=forking: systemd believes that the service starts successfully when the service process fork and the parent process exits. For regular daemons (daemon), unless you are sure that this startup method does not satisfy the requirements, use this type to launch. Using this startup type, you should also specify PIDFile= so that systemd can track the main process of the service.

Type=oneshot: this 1 option applies to services that perform only one task and then exit immediately. You may also need to set RemainAfterExit=yes so that systemd still considers the service to be active after the service process exits.

Type=notify: the same as Type=simple, but the contract service will send a signal to systemd when it is ready. The implementation of this 1 notification is provided by libsystemd-daemon.so.

Type=dbus: if started this way, systemd is considered ready for service when the specified BusName appears on the DBus system bus.

Type=idle: systemd waits for all tasks (Jobs) to complete before starting to execute units of type idle. In addition, other behaviors are similar to Type=simple.

PIDFile: pid file path

ExecStart: the command or script that specifies the startup unit, and the ExecStartPre and ExecStartPost sections specify the script that is user-defined to be executed before or after ExecStart. Type=oneshot allows you to specify multiple user-defined commands that you want to execute sequentially.

ExecReload: specifies the command or script to execute when the cell stops.

ExecStop: specifies the command or script to execute when the cell stops.

PrivateTmp: True means assigning a separate temporary space to a service

Restart: if this option is allowed, the process exits when the service is restarted and the clean up and restart operation is performed via the systemctl command.

RemainAfterExit: if this selection is set to true, the service is considered to be active, even if all processes have exited and the default value is false. This option only needs to be configured if Type=oneshot.

[Install]

Alias: provides the unit with an additional name for spatial separation.

RequiredBy: units are allowed to run the required 1 series of dependency units, and the RequiredBy list gets the dependency information from Require.

WantBy: units are allowed to run the required weak dependency units, and Wantby gets the dependency information from the Want list.

Also: indicates which units are installed or assisted with unit 1.

DefaultInstance: the restriction of the instance unit. This option specifies if the unit is allowed to run the default instance.

3. Reload service

systemctl enable nginx.service

In/etc systemd system/multi - user. target. wants/directory new 1 / usr/lib systemd/system/nginx service file links.

4. Operating services


# Start the service 
$ sudo systemctl start nginx.service
 
# See the log 
$ sudo journalctl -f -u nginx.service
 -  Logs begin at 4 2015-06-25 17:32:20 CST.  - 
6 month  25 10:28:24 Leco.lan systemd[1]: Starting nginx  �  high performance web server ... 
6 month  25 10:28:24 Leco.lan nginx[7976]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
6 month  25 10:28:24 Leco.lan nginx[7976]: nginx: configuration file /etc/nginx/nginx.conf test is successful
6 month  25 10:28:24 Leco.lan systemd[1]: Started nginx  �  high performance web server.
 
# restart 
$ sudo systemctl restart nginx.service
 
# overloading 
$ sudo systemctl reload nginx.service
 
# stop 
$ sudo systemctl stop nginx.service

Related articles: