How to add custom system services to CentOS7 systemd

How to add custom system services to CentOS7 systemd

systemd:

The service systemctl script of CentOS 7 is stored in: /usr/lib/systemd/, which is divided into system (system) and user (user), namely: /usr/lib/systemd/system, /usr/lib/systemd/user

Each service ends with .service and is generally divided into three parts: [Unit], [Service], and [Install]. Take nginx as an example, the specific contents are as follows:

Create a service:

Create the nginx.service file under /usr/lib/systemd/system with the following content (it can also be created under /usr/lib/systemd/usr depending on the application requirements):

[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 brief description of the service
Documentation: Service documentation

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

[Service]
Type: startup type simple, forking, oneshot, notify, dbus

Type=simple (default): systemd assumes that the service will be started immediately. The service process will not fork. If the service is to start other services, do not use this type of startup unless the service is socket activated. Type=forking: systemd considers that the service is started successfully when the service process forks and the parent process exits. For regular daemons, use this type of startup unless you are sure that this startup method cannot meet your needs. When using this start type, you should also specify PIDFile= so that systemd can track the main process of the service. Type=oneshot: This option is suitable for 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 active after the service process exits. Type=notify: Same as Type=simple, but specifies that the service will send a signal to systemd when it is ready. The implementation of this 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: pid file path
ExecStartPre: What to do before starting, in the above example, it is to test the configuration file -t
ExecStart: Start
ExecReload: Reload
ExecStop: Stop
PrivateTmp: True means to allocate independent temporary space to the service

[Install]

WantedBy: The user mode for service installation. Literally, it means who wants to use this service? The target used in the above text is: multi-user.target, which means that the directory that wants to use this service is multi-user. "The above is all my personal understanding and guesswork. If there is anything wrong, please give me your advice." Each .target is actually a collection of links to our unit files. When we execute:

$ sudo systemctl enable nginx.service

A new link to the /usr/lib/systemd/system/nginx.service file will be created in the /etc/systemd/system/multi-user.target.wants/ directory.

Operation Service:

#Start the service$ sudo systemctl start nginx.service

#View the log $ sudo journalctl -f -u nginx.service
-- Logs begin at Thursday 2015-06-25 17:32:20 CST. --
Jun 25 10:28:24 Leco.lan systemd[1]: Starting nginx - high performance web server...
Jun 25 10:28:24 Leco.lan nginx[7976]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jun 25 10:28:24 Leco.lan nginx[7976]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jun 25 10:28:24 Leco.lan systemd[1]: Started nginx - high performance web server.

#Restart$ sudo systemctl restart nginx.service

#Reload $ sudo systemctl reload nginx.service

#Stop$ sudo systemctl stop nginx.service

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to add custom system services to systemd and set custom startup
  • How to configure HTTP/HTTPS proxy in Docker
  • Detailed explanation of setting up a proxy for Docker
  • Detailed explanation of Docker network proxy settings
  • Setting up Docker proxy under CentOS 7 (environment variable configuration of Systemd service under Linux)

<<:  Solution to the initialization error when installing mysql5.7 from rpm package in centos6.5

>>:  VUE implements a Flappy Bird game sample code

Recommend

MySQL changes the default engine and character set details

Table of contents 1. Database Engine 1.1 View dat...

Vue Getting Started with Weather Forecast

This article example shares the specific code of ...

Detailed explanation of common methods of JavaScript arrays

Table of contents Common array methods pop() unsh...

How to use ES6 class inheritance to achieve a gorgeous ball effect

Table of contents introduce Implementation steps ...

How to communicate between WIN10 system and Docker internal container IP

1. After installing the Windows version of Docker...

JavaScript to implement image preloading and lazy loading

This article shares the specific code for impleme...

19 MySQL optimization methods in database management

After MySQL database optimization, not only can t...

Solution to the problem that Docker cannot stop or delete container services

Preface Today, a developer gave me feedback that ...

Linux Domain Name Service DNS Configuration Method

What is DNS The full name of DNS is Domain Name S...

How to build a multi-node Elastic stack cluster on RHEL8 /CentOS8

Elastic stack, commonly known as ELK stack, is a ...

MySQL transaction autocommit automatic commit operation

The default operating mode of MySQL is autocommit...

javascript input image upload and preview, FileReader preview image

FileReader is an important API for front-end file...