Systemd Notes
文章目录
相关命令
systemctl
- 启动控制
systemd-cat
- 输出到 journal
基本操作
systemctl enable <unit>
- systemctl enable –now
- systemctl disable
systemctl mask
- systemctl unmask
电源管理(开关机)
- systemctl reboot
- systemctl poweroff
- systemctl suspend
- systemctl hibernet
查看 log — journalctl
给定 service
1journalctl -u <my.service>只看本次启动
1journalctl -b
systemd 类型
参考
手动运行程序和 systemd 对应关系
普通程序 – simple
- terminal 输入程序
- 结束方式
- 一直运行,或很久才能完成,或者 Ctrl + C 手动结束
后台程序 – forking
- terminal 输入程序
- 结束方式
- 立即返回,但是却实际上在后台运行
配置参数 – oneshot
- terminal 输入程序
- 用途
设置一个参数,没什么长时间需求
- 结束方式
- 需要时间,但是很快完成
特殊类型
- dbus
notify
- 会连接 unix socket,包含于$NOTIFY_SOCKET
forking
- 给定脚本能够自己完成 fork
- 能够捕捉脚本产生的子进程信息
oneshot
- 单次运行的任务,直到完成任务,自己结束
可以有多个 ExecStart
- 会被依次运行
完成后变成 inactive
如果指定 RemainAfterExit=true
- 退出后,仍然是 active,但是没有运行的进程
也可以只有 ExecStop
- 用于再关机前自动执行
simple
- 自动完成 fork
一启动,状态就变成 active,不会阻塞
- 即使脚本还在启动中
脚本完成,变成 inactive
- 没有 RemainAfterExit 选项
不被推荐使用
- 因为不能分辨是启动错误,还是运行一段时间后退出
开机启动
| |
实例
为 clash 编写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17[Unit] Description=Clash Backend After=network.target ConditionPathExists=!/etc/ssh/sshd_not_to_be_run [Service] ExecStart=/home/sawyer/bin/clash -d /home/sawyer/.config/clash ExecStop=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartPreventExitStatus=255 Type=simple Restart=on-failure [Install] WantedBy=multi-user.target验证有效
- start
- stop
- status
restart
- 没有 reload
- 异常退出后,自动重启
没有 DBUS-SESSION 怎么办
❯ systemctl --user start emacs.service Failed to connect to bus: $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR not defined (consider using --machine=<user>@.host --user to connect to bus of other user)
修复命令:
| |
验证成功方法:
| |
You should see something like Linger=yes in the output.
文章作者
上次更新 2025-09-24 (360d44c)