![注销时运行 systemd 服务](https://linux22.com/image/1701051/%E6%B3%A8%E9%94%80%E6%97%B6%E8%BF%90%E8%A1%8C%20systemd%20%E6%9C%8D%E5%8A%A1.png)
我有一个脚本,想要在注销/重启和关机时使用 .service 文件运行/etc/systemd/system
。
[Unit]
Description=My description
DefaultDependencies=no
Before=poweroff.target halt.target reboot.target
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/path/to/myscript.sh
[Install]
WantedBy=poweroff.target halt.target reboot.target
我已经使用 systemctl 启用了该脚本,它在重启和关机时有效,但在注销时无效。这可行吗?
答案1
假设这是一个用户服务,在用户注销时运行(并且,在用户登录时关闭)可以通过(ab)使用来完成ExecStop
:
[Service]
Type=exec
ExecStart=/bin/echo
ExecStop=/path/to/myscript.sh