CentOS7中systemd怎么添加自定义系统服务
创始人
2024-12-22 15:42:31
0次

systemd:

centos 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,即:/usr/lib/systemd/system ,/usr/lib/systemd/user

每一个服务以.service结尾,一般会分为3部分:[unit]、[service]和[install],就以nginx为例吧,具体内容如下:

创建service:

在/usr/lib/systemd/system下创建nginx.service文件内容如下(看应用需求也可以在 /usr/lib/systemd/usr下创建):

[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 : 服务的简单描述
documentation : 服务文档

after= : 依赖,仅当依赖的服务启动之后再启动自定义的服务单元

[service]
type : 启动类型simple、forking、oneshot、notify、dbus

type=simple(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。 type=forking:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 pidfile=,以便systemd能够跟踪服务的主进程。 type=oneshot:这一选项适用于只执行一项任务、随后立即退出的服务。可能需要同时设置 remainafterexit=yes 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。 type=notify:与 type=simple 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 libsystemd-daemon.so 提供。 type=dbus:若以此方式启动,当指定的 busname 出现在dbus系统总线上时,systemd认为服务就绪。

pidfile : pid文件路径
execstartpre :启动前要做什么,上文中是测试配置文件 -t 
execstart:启动
execreload:重载
execstop:停止
privatetmp:true表示给服务分配独立的临时空间

[install]

wantedby:服务安装的用户模式,从字面上看,就是想要使用这个服务的有是谁?上文中使用的是:multi-user.target ,就是指想要使用这个服务的目录是多用户。「以上全是个人理解,瞎猜的,如有不当,请大家多多指教」每一个.target实际上是链接到我们单位文件的集合,当我们执行:

$ sudo systemctl enable nginx.service

就会在/etc/systemd/system/multi-user.target.wants/目录下新建一个/usr/lib/systemd/system/nginx.service 文件的链接。

操作service:

#启动服务
$ sudo systemctl start nginx.service

#查看日志
$ sudo journalctl -f -u nginx.service
-- logs begin at 四 2015-06-25 17:32:20 cst. --
6月 25 10:28:24 leco.lan systemd[1]: starting nginx - high performance web server...
6月 25 10:28:24 leco.lan nginx[7976]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
6月 25 10:28:24 leco.lan nginx[7976]: nginx: configuration file /etc/nginx/nginx.conf test is successful
6月 25 10:28:24 leco.lan systemd[1]: started nginx - high performance web server.

#重启
$ sudo systemctl restart nginx.service

#重载
$ sudo systemctl reload nginx.service

#停止
$ sudo systemctl stop nginx.service

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...