注销时运行 systemd 服务

注销时运行 systemd 服务

我有一个脚本,想要在注销/重启和关机时使用 .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

相关内容