我想安排在笔记本电脑(当前运行 Ubuntu 20.04)上进行定期夜间备份,该笔记本电脑通常处于挂起状态。Systemd 计时器似乎就是这样,但不太管用——系统会在正确的时间唤醒,启动备份脚本,然后检测到盖子已关闭并直接返回睡眠状态。(当笔记本电脑下次从睡眠状态唤醒时,备份将继续)
我有两个 systemd 文件。
/etc/systemd/system/my_backup.timer
:
[Unit]
Description=Run backups
[Timer]
OnCalendar=Sat *-*-* 02:00
WakeSystem=true
[Install]
WantedBy=timers.target
和/etc/systemd/system/my_backup.service
[Unit]
Description=Run backups
[Service]
Type=oneshot
ExecStart=/root/bin/run_backups.sh
我用 启用它systemctl enable my_backup.timer ; systemctl start my_backup.timer
。
当我尝试时,我得到的日志如下所示。前三行是在挂起之前(17:59);我要求它在 18:02 唤醒,它确实做到了。但是它随后直接回到睡眠状态。我相信这是因为 systemd 检测到笔记本电脑盖子已关闭;盖子打开时不会发生这种情况。
Mar 20 17:59:49 redline systemd[1]: Reached target Sleep.
Mar 20 17:59:49 redline systemd[1]: Starting Suspend...
Mar 20 17:59:49 redline systemd-sleep[1575590]: Suspending system...
Mar 20 18:02:05 redline systemd[1]: Starting Load/Save RF Kill Switch Status...
Mar 20 18:02:05 redline systemd[1]: Stopped target Bluetooth.
Mar 20 18:02:05 redline systemd[3277]: Stopped target Bluetooth.
Mar 20 18:02:05 redline systemd[1]: Reached target Bluetooth.
Mar 20 18:02:05 redline systemd[3277]: Reached target Bluetooth.
Mar 20 18:02:05 redline systemd[1]: Started Load/Save RF Kill Switch Status.
Mar 20 18:02:05 redline systemd-sleep[1575590]: System resumed.
Mar 20 18:02:05 redline systemd-sleep[1575642]: /dev/sda:
Mar 20 18:02:05 redline systemd-sleep[1575642]: setting Advanced Power Management level to 0xfe (254)
Mar 20 18:02:05 redline systemd-sleep[1575642]: APM_level = 254
Mar 20 18:02:06 redline systemd[1]: systemd-suspend.service: Succeeded.
Mar 20 18:02:06 redline systemd[1]: Finished Suspend.
Mar 20 18:02:06 redline systemd[1]: Stopped target Sleep.
Mar 20 18:02:06 redline systemd[1]: Reached target Suspend.
Mar 20 18:02:06 redline systemd[1]: Stopped target Suspend.
Mar 20 18:02:06 redline systemd-logind[1661]: Operation 'sleep' finished.
Mar 20 18:02:10 redline systemd[1]: Starting resolvconf-pull-resolved.service...
Mar 20 18:02:10 redline systemd[1]: resolvconf-pull-resolved.service: Succeeded.
Mar 20 18:02:10 redline systemd[1]: Finished resolvconf-pull-resolved.service.
Mar 20 18:02:10 redline systemd[1]: systemd-rfkill.service: Succeeded.
Mar 20 18:02:20 redline systemd[1]: NetworkManager-dispatcher.service: Succeeded.
Mar 20 18:02:32 redline systemd-logind[1661]: Suspending...
Mar 20 18:02:32 redline kernel: Lockdown: systemd-logind: hibernation is restricted; see man kernel_lockdown.7
Mar 20 18:02:32 redline dbus-daemon[1617]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service' requested by ':1.590275' (uid=0 pid=2807467 comm="/usr/sbin/NetworkManager --no-daemon " label="unconfined")
Mar 20 18:02:32 redline kernel: Lockdown: systemd-logind: hibernation is restricted; see man kernel_lockdown.7
Mar 20 18:02:32 redline systemd[1]: Starting Network Manager Script Dispatcher Service...
Mar 20 18:02:32 redline kernel: Lockdown: systemd-logind: hibernation is restricted; see man kernel_lockdown.7
Mar 20 18:02:32 redline kernel: Lockdown: systemd-logind: hibernation is restricted; see man kernel_lockdown.7
Mar 20 18:02:32 redline systemd[1]: Started Network Manager Script Dispatcher Service.
Mar 20 18:02:32 redline kernel: Lockdown: systemd-logind: hibernation is restricted; see man kernel_lockdown.7
Mar 20 18:02:32 redline kernel: Lockdown: systemd-logind: hibernation is restricted; see man kernel_lockdown.7
Mar 20 18:02:32 redline kernel: Lockdown: systemd-logind: hibernation is restricted; see man kernel_lockdown.7
Mar 20 18:02:32 redline systemd[1]: Reached target Sleep.
Mar 20 18:02:32 redline systemd[1]: Starting resolvconf-pull-resolved.service...
Mar 20 18:02:32 redline systemd[1]: Starting Suspend...
我也尝试过systemd-inhibit
,但是将ExecStart
行改为:
ExecStart=systemd-inhibit --what=sleep --who=my.backups --why=running.backups --mode=block /root/bin/run_backups.sh
这会导致错误(我将其解释为“当我们已经进入睡眠时无法抑制睡眠”,但我不完全确定)。
Mar 21 12:13:06 redline systemd-inhibit[1607510]: Failed to inhibit: The operation inhibition has been requested for is already running
Mar 21 12:13:06 redline systemd[1]: my_backup.service: Main process exited, code=exited, status=1/FAILURE
Mar 21 12:13:06 redline systemd[1]: my_backup.service: Failed with result 'exit-code'.
我相信此错误与(撰写本文时打开)中描述的相同systemd 问题。该问题的可能解决方法(sleep 1m
在 systemd-inhibit 之前添加以避免竞争)对我来说不起作用,因为在那之前它早已回到暂停状态。
因此乍一看,WakeSystem=true
与 相结合systemd-inhibit
应该能够完全满足我的要求,但实际上并非如此。但也许我忽略了一些东西 - 例如更好地利用 systemd 目标等?
谢谢你的帮助!
答案1
我有一个类似的设置并使用以下计时器和服务文件,希望这些有所帮助:
/etc/systemd/system/backup.timer
:
[Unit]
Description=Backup
[Timer]
Unit=backup.service
OnCalendar=*-*-* 03:00
WakeSystem=true
Persistent=false
[Install]
WantedBy=multi-user.target
/etc/systemd/system/backup.service
:
[Unit]
Description=Backup
RefuseManualStart=true
RefuseManualStop=true
ConditionACPower=true # ensure power cable plugged in
Wants=network-online.target # wait for network to be available
After=network-online.target
[Service]
Type=oneshot
ExecStart=/bin/systemd-inhibit --what=handle-lid-switch --why=backups /bin/su <my_username> -c "/path/to/backup/script"
我认为这--what=handle-lid-switch
是防止系统立即进入睡眠状态的重要区别。