使用 systemd 延迟系统关闭?

使用 systemd 延迟系统关闭?

将系统关闭延迟任​​意时间的最佳方法是什么systemd

我正在尝试以下单位:

root@ip-172-30-3-65:~# cat /etc/systemd/system/delay_by30.service
[Unit]
Description=Delay Shutdown by 30 sec.
#Before=apache2 nginx php5-fpm

[Service]
Type=oneshot
ExecStart=/bin/echo
ExecStop=/bin/sleep 30

[Install]
WantedBy=multi-user.target

然而我得到的结果非常混乱。对于初学者来说,我的行以某种方式在 on和 my onExecStop上执行,而不是应有的方式。时间戳确认这一点:systemctl startExecStartsystemctl stopjournalctl

root@ip-172-30-3-65:~# systemctl enable delay_by30.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/delay_by30.service to /etc/systemd/system/delay_by30.service.
root@ip-172-30-3-65:~# systemctl start delay_by30.service 
root@ip-172-30-3-65:~# systemctl stop delay_by30.service 
root@ip-172-30-3-65:~# journalctl -n 4 -u delay_by30
-- Logs begin at Fri 2017-01-06 17:05:34 GMT, end at Mon 2017-01-09 15:57:34 GMT. --
Jan 09 15:55:59 ip-172-30-3-65 systemd[1]: Starting Delay Shutdown by 30 sec....
Jan 09 15:56:29 ip-172-30-3-65 systemd[1]: Started Delay Shutdown by 30 sec..
Jan 09 15:57:34 ip-172-30-3-65 systemd[1]: Stopped Delay Shutdown by 30 sec..

答案1

您需要添加RemainAfterExit=yes到您的[Service]部分。现在,systemd 看到您的进程 ( /bin/echo) 退出,此时它认为该服务已退出,并继续停止它。

(你可能也可以没有线路ExecStart,但我没有测试过。)

相关内容