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-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to add custom system services in systemd in CentOS7". In daily operation, I believe many people have doubts about how to add custom system services in systemd in CentOS7. Xiaobian consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how to add custom system services in systemd in CentOS7"! Next, please follow the small series to learn together!

systemd:

The service systemctl script for centos 7 is stored in: /usr/lib/systemd/, divided into system (system) and user (user), i.e.:/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 service:

Create an nginx.service file in/usr/lib/systemd/system with the following contents (see application requirements, you can also create it in/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= : dependency, start custom service units only after dependent services start

[service]

type : startup type simple, forking, oneshot, notify, dbus

type=simple (default): systemd assumes that the service will start immediately. Service processes do not fork. If the service starts other services, do not use this type of startup unless the service is socket-activated. type=forking: systemd considers service startup successful when the service process forks and the parent process exits. For regular daemons, unless you are sure that this boot method does not meet your needs, use this type of boot. Use this boot type with pidfile= so 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 also need to set remain after exit =yes so that systemd still considers the service active after the service process exits. type=notify: Same as type=simple, but the contract service sends a signal to systemd when ready. Implementation of this notification is provided by libsystemd-daemon.so type=dbus: If started in this manner, systemd considers the service ready when the specified busname appears on the dbus system bus.

pidfile: pid file path

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

execstart: Start

execreload: overload

execstop: stop

privatetmp: true indicates that a separate temporary space is allocated to the service

[install]

wantedby: user mode of service installation, literally, who wants to use the service? As used above: multi-user.target means that the directory you want to use this service for is multi-user. "All of the above is personal understanding, guesswork, if there is any improper, please advise" Each.target is actually a collection of links to our unit files, when we execute:

$ sudo systemctl enable nginx.service

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

Operation service:

#Start service $ sudo systemctl start nginx.service#View logs $ sudo journalctl -f -u nginx.service-- logs begin at Thursday 2015-06-25 17:32:20 cst. - -25 Jun 10:28:24 leco.lan systemd[1]: starting nginx - high performance web server... June 25 10:28:24 leco.lan nginx[7976]: nginx: the configuration file /etc/nginx/nginx.conf syntax is okJune 25 10:28:24 leco.lan nginx[7976]: nginx: configuration file /etc/nginx/nginx.conf test is successfulJune 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 This is the end of the study on "how to add custom system services in systemd in CentOS7," hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

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

Development

Wechat

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

12
Report