CentOS7 systemd A way to add custom system services

  • 2021-01-06 00:50:29
  • OfStack


CentOS systemctl script in 7 service: / usr/lib systemd/system (system) and users (user), namely: / usr lib/systemd/system, / usr lib/systemd/user

Each service ends with.service and is usually divided into three parts: [Unit], [Service] and [Install]. Take nginx as an example. The details are as follows:

Create service:

Under/usr lib/systemd/system create nginx. service file content is as follows (see application requirements can also be under/usr lib/systemd/usr create) :

Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
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

Description: A simple description of the service
Documentation: Service documentation

After= : Dependency, starting the custom service unit only after the dependent service is started

Type: Boot types simple, forking, oneshot, notify, dbus


Type=simple (default) : systemd thinks the service will start immediately. The server process will not fork. If the service starts another service, do not start with this type unless the service is socket active. Type=forking: systemd says that the service started successfully when the parent process exits fork. For regular daemons (daemon), use this startup type unless you determine that this startup method does not meet your needs. With this startup type you should also specify PIDFile= so that systemd can track the main process of the service. Type=oneshot: This option is for services that perform only one task and then exit immediately. It may be necessary to set RemainAfterExit=yes at the same time so that systemd considers the service to be active after the server process exits. Type=notify: Same as Type=simple, but it is agreed that the service will send 1 signal to systemd when ready. The implementation of this 1 notification is provided by libsystemd-daemon.so. Type=dbus: If started in this way, systemd considers the service ready when the specified BusName appears on the DBus system bus.


PIDFile: Path to the pid file
ExecStartPre: What to do before starting, above is the test configuration file -t
ExecStart: start
ExecReload: overloading
ExecStop: stop
PrivateTmp: True means to assign a separate temporary space to a service


WantedBy: User mode for service installation, literally, who wants to use the service? multi-user. target means that the directory where you want to use the service is multi-user. target is actually a collection of links to our unit files, and when we implement:

$ sudo systemctl enable nginx.service

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

Operate Service:

# 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: