systemd:如何检查延迟关闭的预定时间?

systemd:如何检查延迟关闭的预定时间?

shutdown -h TIME/+DELAY我有时喜欢用。然而,自从切换到 systemd(在 Ubuntu 上)后,事情似乎发生了很大的变化。

除了先前的关闭命令不再阻止运行新命令这一事实之外,我不知道如何检查当前关闭进程的计划关闭时间。

我以前只是跑去ps aux | grep shutdown看看计划的关闭时间。

现在使用 systemd 它只会显示这样的内容:

root      5863  0.0  0.0  13300  1988 ?        Ss   09:04   0:00 /lib/systemd/systemd-shutdownd

如何查看此类进程的预定关闭时间?

我尝试过shutdown -k,但不是只写一条留言墙,它似乎也将预定的关闭时间更改为现在+1分钟。

答案1

# cat /run/systemd/shutdown/scheduled
USEC=1537242600000000
WARN_WALL=1
MODE=poweroff

USEC 是一个具有微秒精度的 unix 纪元时间戳,因此:

if [ -f /run/systemd/shutdown/scheduled ]; then
  perl -wne 'm/^USEC=(\d+)\d{6}$/ and printf("Shutting down at: %s\n", scalar localtime $1)' < /run/systemd/shutdown/scheduled
fi

将显示类似以下内容:

Shutting down at: Tue Sep 18 03:50:00 2018

系统版本是232-25+deb9u4在 Debian Stretch 上运行。

答案2

最简单:(并在 Debian/Ubuntu 和 RedHat 上工作)

date --date @$(head -1 /run/systemd/shutdown/scheduled |cut -c6-15)

答案3

所有系统版本

使用org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdownD-Bus接口:

USECS=$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown | cut -d ' ' -f 3)
SECS=$((USECS / 1000000))
date --date=@$SECS

仅 systemd 版本 < 2015 年 5 月

# systemctl status systemd-shutdownd.service

你应该看到这样的东西:

● systemd-shutdownd.service - Delayed Shutdown Service
Loaded: loaded (/lib/systemd/system/systemd-shutdownd.service; static; vendor preset: enabled)
Active: active (running) since Tue 2015-09-15 09:13:11 UTC; 12s ago
Docs: man:systemd-shutdownd.service(8)
Main PID: 965 (systemd-shutdow)
Status: "Shutting down at Tue 2015-09-15 09:18:11 UTC (poweroff)..."
CGroup: /system.slice/systemd-shutdownd.service
       └─965 /lib/systemd/systemd-shutdownd

StatusShutting down at Tue 2015-09-15 09:18:11 UTC (poweroff)...

答案4

我遇到了同样的问题,找到了另一种方法来检查关闭计划。

当你设定关闭计划时,将向所有登录的人发送一条消息,并将其消息权限设置为“是”。每次调用 wall 时,都会将通知写入系统日志。要搜索系统日志,你可以运行命令journalctl -u systemd-shutdownd,该-u选项可以按单位过滤日志。

当您运行时journalctl -u systemd-shutdownd,它将显示关闭详细信息,如下所示:

[root@dev log]# journalctl -u systemd-shutdownd
-- Logs begin at Mon 2017-06-12 09:39:34 CST, end at Mon 2017-06-12 14:05:04 CST. --
Jun 12 09:39:50 dev.local systemd[1]: Started Delayed Shutdown Service.
Jun 12 09:39:50 dev.local systemd[1]: Starting Delayed Shutdown Service...
Jun 12 09:39:50 dev.local systemd-shutdownd[1249]: Shutting down at Mon 2017-06-12 21:00:00 CST (poweroff)...
Jun 12 09:55:59 dev.local systemd-shutdownd[1249]: Shutdown canceled.
Jun 12 09:56:07 dev.local systemd[1]: Started Delayed Shutdown Service.
Jun 12 09:56:07 dev.local systemd[1]: Starting Delayed Shutdown Service...
Jun 12 09:56:07 dev.local systemd-shutdownd[2885]: Shutdown canceled.
Jun 12 11:54:15 dev.local systemd[1]: Started Delayed Shutdown Service.
Jun 12 11:54:15 dev.local systemd[1]: Starting Delayed Shutdown Service...
Jun 12 11:54:15 dev.local systemd-shutdownd[3178]: Shutting down at Mon 2017-06-12 20:00:00 CST (poweroff)...

相关内容