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 server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
//pid存放在nginx的logs文件夹可以防止pid文件丢失问题,除了此处还需要修改nginx.conf 的pid参数路径和此处一致
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
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 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
#停止
systemd科普
科普1:浅析 Linux 初始化 init 系统,Systemd
科普2:Getting Started with systemd
转载自(https://my.oschina.net/liucao/blog/470458)
CentOs7 systemd添加自定义系统服务的更多相关文章
- systemd添加自定义系统服务设置自定义开机启动
1.服务权限 systemd有系统和用户区分:系统(/user/lib/systemd/system/).用户(/etc/lib/systemd/user/).一般系统管理员手工创建的单元文件建议存放 ...
- CentOS 7 systemd添加自定义系统服务
systemd: CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,即:/usr/lib/systemd/syste ...
- CentOS7利用systemctl添加自定义系统服务【转】
systemctl enable name.service 设置开机启 systemctl disable name.service 删除开机启动指令 systemctl list-units --t ...
- 在CentOS 7上利用systemctl添加自定义系统服务 /usr/lib/systemd/
在CentOS 7上利用systemctl添加自定义系统服务[日期:2014-07-21] 来源:blog.csdn.net/yuanguozhengjust 作者:yuanguozhengjust ...
- 基于CentOS7系统添加自定义脚本服务及参数说明【转】
概述 centos6如果要添加自定义脚本服务只需要把脚本放到/etc/init.d然后授权后用chkconfig添加后就可以管理了,那么centos7又是怎么添加自定义脚本服务呢? CentOS7添加 ...
- [实践] Android5.1.1源码 - 在Framework中添加自定义系统服务
前言 本文的方法要修改Android源码.但只要将系统服务写出来,也可以注入system_server进程将系统服务加载到system_server中. 步骤 1. 添加一个aidl文件,我将aidl ...
- [转]CentOS7利用systemctl添加自定义系统服务
原文:https://www.cnblogs.com/saneri/p/7778756.html CentOS7自定义系统服务 CentOS7的服务systemctl脚本存放在:/usr/lib/sy ...
- CentOS7 利用systemctl添加自定义系统服务
一.命令systemctl介绍 CentOS 7.0中已经没有service命令,而是启用了systemctl服务器命令,它实际上将 service 和 chkconfig 这两个命令组合到一起. 命 ...
- CentOS7利用systemctl添加自定义系统服务
CentOS7的每一个服务以.service结尾,一般会分为3部分:[Unit].[Service]和[Install] 转载于互联网 [Unit] 部分主要是对这个服务的说明,内容包括Descrip ...
随机推荐
- js笔记(制作一个简单的计数器)
首先编写静态页中的按钮: <input id="result" type="button" value="该程序已经运行了0秒!"/ ...
- php编译安装php-5.6
#php编译安装php-5.6 ,Nginx+php使用 #!/bin/sh #php编译安装php-5.6 ,Nginx+php使用 #定义函数,默认绿色输出 '#' 开头为红色 function ...
- 最短路洛谷P2384
题目背景 狗哥做烂了最短路,突然机智的考了Bosh一道,没想到把Bosh考住了...你能帮Bosh解决吗? 他会给你100000000000000000000000000000000000%10金币w ...
- PHP扩展开发 第一课 为什么要写扩展及hello world
PHP扩展开发其实很简单.那为什么要扩展开发呢. 这里咱们以实际的案例进行对比. 第一步,进入 php源码包 http://www.php20.com/forum.php?m ... =159&a ...
- 雅虎WEB前端网站优化 -- 34条军规
雅虎给出了优化网站加载速度的34条法则(包括Yslow规则22条) 详细说明,下载转发 ponytail 的译文(来自帕兰映像). 1.Minimize HTTP Requests 减少HTTP请求 ...
- idea从git上拉取并管理项目
1:idea从git上拉取项目 (1)FILE --> New --> Project from Version Control --> Git (2):输入项目的Https SSH ...
- [置顶]
spring集成mina 实现消息推送以及转发
spring集成mina: 在学习mina这块时,在网上找了很多资料,只有一些demo,只能实现客户端向服务端发送消息.建立长连接之类.但是实际上在项目中,并不简单实现这些,还有业务逻辑之类的处理以及 ...
- springMVC使用jsp:include嵌入页面的两种方式
1.静态嵌入子页面 <%@ include file="header.jsp" %> 静态嵌入支持 jsp . html . xml 以及纯文本. 静态嵌入在编译时 ...
- 多线程编程学习笔记——async和await(一)
接上文 多线程编程学习笔记——任务并行库(一) 接上文 多线程编程学习笔记——任务并行库(二) 接上文 多线程编程学习笔记——任务并行库(三) 接上文 多线程编程学习笔记——任务并行库(四) 通过前面 ...
- OBS源码解析(1)main函数
int main(int argc, char *argv[]){#ifndef _WIN32 signal(SIGPIPE, SIG_IGN);#endif #ifdef _WIN32 /*Open ...