centos7上systemd详解

centos7上systemd详解

96 六弦极品 关注html

2018.07.21 16:39* 字数 1063 阅读 291评论 0喜欢 0nginx

CentOS 7继承了RHEL 7的新的特性,如强大的systemd, 而systemd的使用也使得以往系统服务的/etc/init.d的启动脚本的方式就此改变, 也大幅提升了系统服务的运行效率。但服务的配置和以往也发生了极大的不一样,同时变的简单而易用了许多。web

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

1、配置文件

这里先说明一下unit的文件位置,通常主要有三个目录:socket

/lib/systemd/system
/run/systemd/system
/etc/systemd/system

这三个目录的配置文件优先级依次从低到高,若是同一选项三个地方都配置了,优先级高的会覆盖优先级低的。 系统安装时,默认会将unit文件放在/lib/systemd/system目录。若是想要修改系统默认的配置,好比nginx.service,通常有两种方法:ide

一、在/etc/systemd/system目录下建立nginx.service文件,里面写上咱们本身的配置。
在/etc/systemd/system下面建立nginx.service.d目录,在这个目录里面新建任何以.conf结尾的文件,而后写入本身的配置。推荐这种作法。测试

二、/run/systemd/system这个目录通常是进程在运行时动态建立unit文件的目录,通常不多修改,除非是修改程序运行时的一些参数时,即Session级别的,才在这里作修改。ui

2、服务配置

每个服务以.service结尾,通常会分为3部分:[Unit]、[Service]和[Install],nginx为例,具体内容以下:centos7

[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
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=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

3、配置项说明

下面分别解释下着三部分的含义spa

  • [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其实是连接到咱们单位文件的集合,当咱们执行:
systemctl enable nginx.service

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

4、操做示例

下面是几个最经常使用的service操做:

自启动
systemctl enable nginx.service

禁止自启动
systemctl disable nginx.service

启动服务
systemctl start nginx.service

中止服务
systemctl stop nginx.service

重启服务
systemctl restart nginx.service

查看Unit定义文件
systemctl cat nginx.service

编辑Unit定义文件
systemctl edit nginx.service

从新加载Unit定义文件
systemctl reload nginx.service

列出已启动的全部unit,就是已经被加载到内存中
systemctl list-units

列出系统已经安装的全部unit,包括那些没有被加载到内存中的unit
systemctl list-unit-files

查看服务的日志
journalctl -u nginx.service    # 还能够配合`-b`一块儿使用,只查看自本次系统启动以来的日志

查看全部target下的unit
systemctl list-unit-files --type=target

查看默认target,即默认的运行级别。对应于旧的`runlevel`命令
systemctl get-default

设置默认的target
systemctl set-default multi-user.target

查看某一target下的unit
systemctl list-dependencies multi-user.target

切换target,不属于新target的unit都会被中止
systemctl isolate multi-user.target

关机
systemctl poweroff   
 
重启
systemctl reboot       

进入rescue模式
systemctl rescue

参考资料

https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Unit_Files.html
https://www.nginx.com/resources/wiki/start/topics/examples/systemd/
http://time-track.cn/systemd-introduction.html

小礼物走一走,来简书关注我

相关文章
相关标签/搜索