Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add Custom system Services to systemd in CentOS7

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to add custom system services to systemd in CentOS7. I hope you will get something after reading this article. Let's discuss it together.

Systemd:

The service systemctl script of CentOS 7 is stored in: / usr/lib/systemd/, can be divided into system (system) and user (user), that is, / 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 details are as follows:

Create a service:

The contents of the nginx.service file created under / usr/lib/systemd/system are as follows (see application requirements can also be created under / usr/lib/systemd/usr):

[Unit] Description=nginx-high performance web serverDocumentation= http://nginx.org/en/docs/After=network.target remote-fs.target nss-lookup.target [Service] Type=forkingPIDFile=/run/nginx.pidExecStartPre=/usr/sbin/nginx-t-c / etc/nginx/nginx.confExecStart=/usr/sbin/nginx-c / etc/nginx/nginx.confExecReload=/bin/kill-s HUP $MAINPIDExecStop=/bin/kill-s QUIT $MAINPIDPrivateTmp=true [Install] WantedBy=multi-user.target

[Unit]

Description: a brief description of the service

Documentation: service documentation

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

[Service]

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

Type=simple (default): systemd believes that the service will start immediately. The service process will not fork. If the service is going to start other services, do not start using 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 meet the requirements, you can use this type of startup. Using this startup type, you should also specify PIDFile=, so that systemd can track the main process of the service. Type=oneshot: this option applies to services that perform only one task and then exit immediately. You may need to set RemainAfterExit=yes at the same time so that systemd still thinks the service is active after the service process exits. Type=notify: same as Type=simple, but the contract service sends 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 thinks the service is ready when the specified BusName appears on the DBus system bus.

PIDFile: pid file path

ExecStartPre: what to do before startup, the above is the test configuration file-t

ExecStart: start

ExecReload: overload

ExecStop: stop

PrivateTmp:True represents a separate temporary space allocated to the service

[Install]

WantedBy: the user mode of the service installation, literally, who wants to use the service? What is used above is: multi-user.target, which means that the directory that wants to use the service is multi-user. "the above is all personal understanding, wild guess, if there is anything wrong, please give us a lot of advice." each .target is actually linked to a collection of 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.

Action Service:

# start the service $sudo systemctl start nginx.service# to view logs $sudo journalctl-f-u nginx.service-- Logs begin at four 2015-06-25 17:32:20 CST. -June 25 10:28:24 Leco.lan systemd [1]: Starting nginx-high performance web server...6 monthly 25 10:28:24 Leco.lan nginx [7976]: nginx: the configuration file / etc/nginx/nginx.conf syntax is ok6 25 10:28:24 Leco.lan nginx [7976]: nginx: configuration file / etc/nginx/nginx.conf test is successful6 25 10:28:24 Leco.lan systemd [1]: Started nginx-high performance web server.# restart $sudo systemctl restart Nginx.service# overload $sudo systemctl reload nginx.service# stop $sudo systemctl stop nginx.service finished reading this article I believe you have a certain understanding of "how to add custom system services to systemd in CentOS7". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report