CentOS7 systemd添加自定义系统服务的方法
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 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 : 服务的简单描述 Documentation : 服务文档 After= : 依赖,仅当依赖的服务启动之后再启动自定义的服务单元 [Service] Type : 启动类型simple、forking、oneshot、notify、dbus
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 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.#重启$ sudo systemctl restart nginx.service#重载$ sudo systemctl reload nginx.service#停止$ sudo systemctl stop nginx.service 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持VEVB武林网。 注:相关教程知识阅读请移步到频道。 |