如果没有用户登录,如何关闭电脑?

Bet*_*nos 4 linux shutdown login timeout

如果我错过了任何现有的答案或解决方案,我很抱歉 - 我的搜索尚未提出任何可行的解决方案。

问题:

我的电脑包含 Windows 和 Linux,其中 Linux 是主要操作系统。Windows 偶尔会被使用,并且倾向于在关机时重新启动,安装一些更新,然后真正关机。但是,由于 Linux 是主要引导目标,因此这种重新引导然后关闭的方式将引导至 Linux,并在登录时无限期地停留在 Linux 中。我希望它在 5 或 10 分钟没有登录和关闭后超时。

解决方案/尝试:

我遇到过在 /etc/systemd/login.conf 中设置超时的情况

IdleAction=poweroff
IdleActionSec=10min
Run Code Online (Sandbox Code Playgroud)

然而,即使我正在积极打字,这也会在 10 分钟后终止我的会话。

我还发现了这篇文章,关于定期检查用户是否登录然后关闭。不幸的是,我从未编写过 bash 脚本或设置过 systemd 计时器,这就是为什么我对这个答案有点茫然。

我的设置:我正在运行 Arch Linux,登录是基于控制台的 tty 登录。

感谢您的任何提示或建议。

gap*_*psf 5

创建两个 systemd 单元和一个脚本。

第一单元:

/etc/systemd/system/are-users-logged.timer
[Unit]
Description=Start check for logged users

[Timer]
OnBootSec=15min
Unit=shutdown-ifno-logged-users
[Install]
WantedBy=timers.target
Run Code Online (Sandbox Code Playgroud)

shutdown-ifno-logged-users.service开机15分钟后运行一次。

第二单元:

/etc/systemd/system/shutdown-ifno-logged-users.service
[Unit]
Description=Shutdown if there are no logged users

[Service]
Type=oneshot
User=root
ExecStart=script_to_check_logged_users
Run Code Online (Sandbox Code Playgroud)

启动脚本来检查登录的用户。

在您的脚本中,如果没有用户登录,loginctl --no-pager list-users则解析或什至关闭的输出。who -q

如果您想防止在有人登录后重新启动,然后在触发计时器之前注销,您可以尝试使用类似的方法are-users-logged.timer来停止。但我想如果用户不是 root 由于密码询问可能会出现问题。/etc/profile.d/somescript.shsudo systemctl stop are-users-logged.timer

最好在 /tmp 中创建一些带有echofrom 的文件/etc/profile.d/somescript.sh来指示有人已登录,并检查script_to_check_logged_users它是否存在并跳过关闭。