systemd

"运行级别":
systemctl get-default                      #查看当前类似sysv中的"运行级别"
systemctl isolate shutdown.target   #切换到shutdown.target服务组(这个服务组其实就是关机)

杂项:
systemctl reboot                 # 类似的有 poweroff / halt(cpu停止) / suspend / hibernate / hybrid-sleep / resue(单用户)
systemd-analyze [obj]         # 启动耗时. obj: blame(查看每个服务启动耗时) / critical-chain(瀑布流, 后面可选服务,例如critical-chain atd.service)
hostnamectl                        #查看主机名, 如果加 set-hostname <name> 则表示设置主机名
localectl                              #查看本地化设置. 后面可以跟类似 set-locale LANG=en_GB.utf8 或 set-keymap en_GB 的参数来设置locale
timedatectl                          #查看时区设置. list-timezones 列出所有可用时区, 设置时区: set-timezone America/New_York 设置时间: set-time YYYY-MM-DD/set-time HH:MM:SS
systemctl list-units              #列出运行的unit, --all 包括启动失败或没配置文件的 --all --state=inactive所有没运行的unit --failed失败的 --type=service仅服务(unit有12种类型)
systemctl is-active <unit>   #判断状态, 类似的动作有: is-failed / is-enabled

管理unit:
systemctl start apache.service                                        # 立即启动一个服务, 类似的有 stop/restart/kill(杀死所有子进程)
systemctl reload apache.service                                     # 重新加载一个服务的配置文件
systemctl daemon-reload                                                # 重载所有修改过的配置文件
systemctl show httpd.service                                           # 显示某个 Unit 的所有底层参数
systemctl show -p CPUShares httpd.service                   # 显示某个 Unit 的指定属性的值
systemctl set-property httpd.service CPUShares=500     # 设置某个 Unit 的指定属性

systemctl list-dependencies nginx.service      #列出依赖, --all target的依赖也会列出
systemctl list-unit-files                                    #列出配置文件状态; --type=<类别> ; static:该配置文件没有[Install]部分, masked:禁止建立启动链接
systemctl cat atd.service                                #查看某unit配置文件


Unit的日志:
journalctl                         #查看所有日志(默认情况下 ,只保存本次启动的日志); 附注1:

journalctl /usr/lib/systemd/systemd     #查看指定服务的日志
journalctl _PID=1                                 # 查看指定进程的日志,类似的 _UID=<uid>

附注1:
   -k 内核日志;
   -b 本次启动; -b -1上次启动日志;
   --since="2012-10-30 18:17:16";
   -n 20 最新20条;
   -f 滚动显示;
   -u nginx.service 指定服务;
   -p <日志级别> 0: emerg/ 1: alert /2: crit /3: err /4: warning /5: notice /6: info /7: debug
   --no-pager 不分页
   -o json 指定格式,如json/ json-pretty 等
   --disk-usage 日志占的空间
   --vacuum-size=1G 指定日志文件占据的最大空间
   --vacuum-time=1years 最大保存一年 

Unit文件位置:
   CentOS7:
        /etc/systemd/system/ 存放很多软链接
        /usr/lib/systemd/system/ 真身, systemctl enable命令其实就是 将真身链接到 /etc/systemd/system/
   Debian9:
       /etc/systemd/system/ 位置同CentOS, 存放很多软链接
       /lib/systemd/system/ 真身

Unit定义:

[Unit] #启动顺序与依赖关系
Description=描述
After=network.target   #target或service,可以有多个(空格分隔)
Before=要求在什么之前启动, After和before只涉及启动顺序,不涉及依赖
Wants=弱依赖, 即该依赖失败或停止运行,不影响我继续执行
Requires=强依赖,即该依赖启动失败或异常退出,会导致我退出

[Service] #启动行为
User=以什么用户运行服务(Group=用于指定运行服务的组)
EnvironmentFile=从文件读取环境, =号后面如果有-号,则表示抑制错误,下同
Environment=ENV01=v01 ENV02=v02 多个环境变量用空格分隔
ExecStart=启动我时,需要执行的命令. 类似的字段有: ExecReload/ ExecStop / ExecStartPre / ExecStartPost / ExecStopPost
Type=启动类型 simple(默认值)/forking/oneshot/dbus/notify/idle. 附注*2*
KillMode=如何停止服务. 附注*3*
Restart=重启方式, 对于守护进程,推荐设为on-failure。对于那些允许发生错误退出的服务,可以设为on-abnormal 附注*4*
RestartSec=42s 重启服务之前需要等待42秒
RemainAfterExit=yes #进程退出后,仍然能执行ExecStop等指定的命令

[Install] #定义如何安装这个配置文件,即怎样做到开机启动
WantedBy=multi-user.target #在multi-user.target服务组启动后启动. systemctl enable sshd.service命令时,我的一个符号链接,就会放在/etc/systemd/system目录下面的multi-user.target.wants子目录之中

例子:
# cat /usr/lib/systemd/system/hehe.service
[Unit]
After=network.service
[Service]
ExecStart=/usr/local/bin/hehe_bin
[Install]
WantedBy=multi-user.target

执行: #systemctl enable hehe.service; systemctl start hehe.service


附注:
2: oneshot: 类似simple,但systemd等待它执行完才会执行其他的服务,也就是说不需要长期运行
    dbus: 类似于simple,但会等待 D-Bus 信号后启动
    notify: 类似于simple,启动结束后会发出通知信号,然后 Systemd 再启动其他服务
    idle: 类似于simple,但是要等到其他任务都执行完,才会启动该服务。一种使用场合是为让该服务的输出,不与其他服务的输出相混合

3: control-group: 默认值,杀死控制组内的所有子进程
    process: 杀死主进程
    mixed:主进程将收到 SIGTERM 信号,子进程收到 SIGKILL 信号
    none:没有进程会被杀掉,只是执行服务的 stop 命令。

4: no(默认值):退出后不会重启
    on-success:只有正常退出时(退出状态码为0),才会重启
    on-failure:非正常退出时(退出状态码非0),包括被信号终止和超时,才会重启
    on-abnormal:只有被信号终止和超时,才会重启
    on-abort:只有在收到没有捕捉到的信号终止时,才会重启
    on-watchdog:超时退出,才会重启
    always:不管是什么退出原因,总是重启

服务组定义(示例):

$ systemctl cat multi-user.target

[Unit]
Description=Multi-User System
Documentation=man:systemd.special(7)
Requires=basic.target
Conflicts=rescue.service rescue.target
After=basic.target rescue.service rescue.target
AllowIsolate=yes  #允许切换



原文地址:https://www.cnblogs.com/mind-water/p/10737170.html