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

JS removeAttribute() method to delete an attribute of an element

In JavaScript, use the removeAttribute() method o...

A brief introduction to the command line tool mycli for operating MySQL database

GitHub has all kinds of magic tools. Today I foun...

About the overlap of margin value and vertical margin in CSS

Margin of parallel boxes (overlap of double margi...

CSS and HTML and front-end technology layer diagram

The relationship between Javascript and DOM is ve...

Detailed explanation of Vue data proxy

Table of contents 1. What I am going to talk abou...

The use of MySQL triggers and what to pay attention to

Table of contents About Triggers Use of triggers ...

Two ways to open and close the mysql service

Method 1: Use cmd command First, open our DOS win...

HTML+CSS to implement the sample code of the navigation bar drop-down menu

Effect The pictures in the code can be changed by...

A quick solution to accidentally delete MySQL data (MySQL Flashback Tool)

Overview Binlog2sql is an open source MySQL Binlo...

How to check if data exists before inserting in mysql

Business scenario: The visitor's visit status...

HTTP Status Codes

This status code provides information about the s...

Introduction to installing JDK under Linux, including uninstalling OpenJDK

1. View openjdk rpm -qa|grep jdk 2. Delete openjd...

Implementation steps for enabling docker remote service link on cloud centos

Here we introduce the centos server with docker i...