参考
单元
在systemd中,服务、挂载等资源统一被称为单元,所以systemd中有许多单元类型,服务单元文件的扩展名是.service,同脚本的功能相似。例如有查看、启动、停止、重启、启用或者禁止服务的参数。
systemd单元存放位置
- 默认单元文件安装目录:/usr/lib/systemd/system/
- 单元运行时创建,优先于默认单元文件:/run/systemd/system
- 系统管理员创建和管理的单元目录,优先级最高:/etc/systemd/system
服务管理
使用systemcl命令可以控制服务,service命令和chkconfig命令依然可以使用,但是主要是出于兼容的原因,应该尽量避免使用
- 服务名可以简写
systemctl stop bluuetooth.service 《==》systemctl stop bluetooth - 启动: systemctl start name.service
- 关闭: systemctl stop name.service
- 重启:systemctl restart name.service
- 仅当服务运行的时候,重启服务: systemctl try-restart name.service
- 重新加载服务配置文件:systemctl relaod name.service
- 检查服务状态:systemctl status name.service
- 列出所有已经加载的单元systemctl list-units --type service --all
- 允许服务开机启动 systemctl enable name.service
- 禁止服务开机启动 systemctl disable name.service
- 检查服务开机启动状态 systemctl status name.service 或者systemctl is-enabled name.service
- 列出所有服务并且检查是否开机启动 systemctl list-unit-files --type service
target
systemd 使用targe代替runlevels,默认的两个target:
- multi-user.target: analogous to runlevel 3
- graphical.target: analogous to runlevel 5
设置默认的target
- ln -sf /lib/systemd/system/<target name>.target /etc/systemd/system/default.target
- ll /etc/systemd/system/default.target
target管理
查看已经加载的target
- systemctl list-units --type target查看可以用的target
- systemctl list-units --type target --all 可以查看未激活的target
默认target管理
- systemctl get-default
- systemctl set-default graphical.target
紧急救援模式
- systtmctl emergency
开机关机休眠管理
使用systemctl替换一些列的电源管理命令,原有的命令依旧可以使用,但是建议尽量不用使用。systemctl和这些命令的对应关系为:
- hatl,systemctl halt停止系统
- poweroff,systemctl poweroff关闭系统,关闭系统电源。
- reboot,systemctl reboot重启系统
- pm-suspend,systemctl suspend暂停系统
- pm-hibernate,systemct lhibernate休眠系统
- pm-suspend-hybrid,systemctl hybrid-sleep暂停并休眠系统
创建和修改单元文件
格式:unit_name.type_extension
Unit
基本信息,单元描述,知道单元行为,配置单元和其他单元的依赖性
Description=Multi-User System
Documentation=man.systemd.special(7)
Requires=basic.target
Conflicts=rescue.service rescure.target
After=basic.target rescue.service rescue.target
AllowIsolate=yes
- Requires
依赖,Requires=basic.target
multi-user.target 启动的时候 basic.target 也必须被启动;另外 basic.target 停止的时候,multi-user.target 也必须停止 - Wants比Requires选项依赖性要弱很多,如果列表之中的的单元启动失败,不会对其他单元造成影响,这是推荐的建立自定义单元依赖性的方式。
- Conflicts定义单元冲突关系,和Requires相反。
- After定义在那些单元之后启动,本单元只在制定的单元启动之后启动,不像Requires选项,After选项不明确激活特定的单元,Before选项则是有相反的功能。
unittype
具体的类型,可能是Service,或者target
Install
包含systemctl enable或者disable的命令
Wants
需要启动的依赖单元
Mount
挂载,相当于:mount –t debugfs /sys/kernel/debug debugfs
[Mount]
What=debugfs
Where=/sys/kernel/debug
Type=debugfs
添加单元文件后的处理
- systemctl daemon-reload
- 启动服务:systemctl start name.service
坑
systemd 服务模块最大打开文件数默认为1024
参考文档:https://blog.csdn.net/gzliudan/article/details/51754701
- vi /etc/systemd/user.conf 与/etc/systemd/system.conf 修改如下参数:
DefaultLimitNOFILE=655350
DefaultLimitNPROC=655350
- 立即对后续使用管理的进程生效,历史的需要重启进程才生效
systemctl daemon-reexec