检测待处理的计划关闭 Ubuntu Server 16.04.1

检测待处理的计划关闭 Ubuntu Server 16.04.1

在 systemd 出现之前,人们可以在进程列表中看到关闭进程(例如ps -ef | grep shutdown)。但是现在,我能找到的最新建议是使用systemctl status,但我运气不佳:

root@m________a:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.1 LTS
Release:    16.04
Codename:   xenial
root@m________a:~# shutdown -r 14:00
Shutdown scheduled for Mon 2016-10-10 14:00:00 BST, use 'shutdown -c' to cancel.
root@m________a:~# ps -ef | grep "shutdown"
root     10584 10508  0 13:44 pts/0    00:00:00 grep --color=auto shutdown
root@m________a:~# systemctl status systemd-halt.service
● systemd-halt.service - Halt
   Loaded: loaded (/lib/systemd/system/systemd-halt.service; static; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:systemd-halt.service(8)
root@m________a:~# systemctl status systemd-reboot.service 
● systemd-reboot.service - Reboot
   Loaded: loaded (/lib/systemd/system/systemd-reboot.service; static; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:systemd-halt.service(8)
root@m________a:~# systemctl status systemd-poweroff.service 
● systemd-poweroff.service - Power-Off
   Loaded: loaded (/lib/systemd/system/systemd-poweroff.service; static; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:systemd-halt.service(8)

如果有人能解释我如何检测待定的计划关机,以及为什么这种关机似乎没有显示出来,我将非常感激systemctl

答案1

您可以使用

cat /run/systemd/shutdown/scheduled

这已添加到 systemd 220 版本中,如下所述: https://www.phoronix.com/scan.php?page=news_item&px=Systemd-Kill-Shutdownd

答案2

systemctl status systemd-shutdownd.service

手册页对于服务:

systemd-shutdownd.service是一个实现计划关闭的系统服务,由shutdown(8)公开。systemd-shutdownd.service它会根据请求自动激活,并在未使用时自行终止。


不活动时:

$ systemctl status systemd-shutdownd.service
● systemd-shutdownd.service - Delayed Shutdown Service
   Loaded: loaded (/lib/systemd/system/systemd-shutdownd.service; static; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:systemd-shutdownd.service(8)

激活时:

 $ sudo shutdown +100
 Shutdown scheduled for ma 2016-10-10 17:12:56 CEST, use 'shutdown -c' to cancel.
 $ 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 ma 2016-10-10 15:32:56 CEST; 3s ago
     Docs: man:systemd-shutdownd.service(8)
 Main PID: 3129 (systemd-shutdow)
   Status: "Shutting down at Mon 2016-10-10 17:12:56 CEST (poweroff)..."
   CGroup: /system.slice/systemd-shutdownd.service
           └─3129 /lib/systemd/systemd-shutdownd

okt 10 15:32:56 x systemd[1]: Started Delayed Shutdown Service.
okt 10 15:32:56 x systemd[1]: Starting Delayed Shutdown Service...
okt 10 15:32:56 x systemd-shutdownd[3129]: Shutting down at Mon 2016-1...
Hint: Some lines were ellipsized, use -l to show in full.

答案3

在较新的系统上(运行systemd220 及以上版本,请检查systemd --version),systemd-shutdownd.service已被删除,并且简单的方法是 cat /run/systemd/shutdown/scheduled。如果文件存在,则关闭正在等待,并且文件将包含类似

USEC=1547919000000000
WARN_WALL=1
MODE=poweroff

我写了一个小脚本来处理这种情况:

#!/usr/bin/env bash

if [ -r /run/systemd/shutdown/scheduled ]; then
    source /run/systemd/shutdown/scheduled;
    echo "System will shutdown to mode=$MODE at $(date --date="@$(( USEC / 1000000 ))")";
    exit 0;
else
    echo "No pending shutdown";
    exit 1;
fi

进一步阅读:

答案4

我发现,如果发出了shutdown -c来取消先前安排的关机,PerlDuck脚本不会显示“没有待处理的关机”。/run/systemd/shutdown中的计划文件仍将保留(Ubuntu 16.04),因此检查它总是会给我旧的答案。当然,如果一开始没有安排任何事,它就可以正常运行。

我觉得从技术上讲,有一种比我使用字符串比较更好的方法可以做到这一点;但只需稍加修改来检查 busctl get-property 给出的结果,它就可以为我工作:

string="$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown)"
if [[ "$string" =~ \ 0$ ]]; then
    echo "No pending shutdown"
    exit 1;
else
    source /run/systemd/shutdown/scheduled
    echo "System will shutdown to mode=$MODE at $(date --date="@$(( USEC / 1000000 ))")";
    exit 0;
fi

相关内容