百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>服务器> CentOS7 systemd增加自定义系统服务的要领
分享文章到:

CentOS7 systemd增加自定义系统服务的要领

发布时间:05/13 来源:未知 浏览: 关键词:

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://www.baidow.com/tupian/lupian/20190513/bui05m0mywg
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~10元,土豪随意,感谢您的阅读!

共有152人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板