4

I am sorry if I have missed any existing answers or solutions - my searching did not bring up any viable solutions yet.

The issue:

My pc contains Windows and Linux, with Linux being the primary OS. Windows is used occasionally and has got the tendency of doing a reboot upon shutdown, to install some updates and then really shutdown. However, as Linux is the primary boot target, this reboot-then-shutdown will boot into Linux, where it will sit at the logon indefinitely. I want it to time out after 5 or 10 minutes of no login and shutdown.

The solutions / attempts:

I have come across setting a timeout in /etc/systemd/login.conf

IdleAction=poweroff
IdleActionSec=10min

However, this kills my session after 10 minutes even if I am actively typing.

I have also found this post, about regularly checking if a user is logged in and then shutting down. Unfortunately, I have never written a bash script or set up systemd timers, which is why I am at a bit of a loss with this answer.

My setup: I am running Arch Linux, with the login being the console-based tty-login.

Thank you for any hints or recommendations.

8
  • 1
    If you login but do nothing, should that stop the idle timer? If you login and logout again, should that restart the idle timer? Commented Sep 11, 2022 at 7:52
  • If I log in, it should stop the timer. The only scenario I want to catch is when my pc reboots after a long Windows update phase and sits idly at the login. This usually happens when I try to shut it down in the evening, which means I only find out that it has been sitting there idly (with an active screen) the next day or day after.
    – Betaminos
    Commented Sep 11, 2022 at 7:57
  • 1
    Also consider Linux may be in the middle of some system background task (cron, systemd) that you do not want to break. Commented Sep 11, 2022 at 8:05
  • 1
    @Paul_Pedant It should be safe in almost all instances to shut down a Linux system, as long as you use the normal shutdown method (i.e. let your init manage the shutdown). Whatever is running in the background should be able to clean up if it needs to.
    – forest
    Commented Sep 11, 2022 at 8:18
  • 1
    @Paul_Pedant Wouldn't that apply to any shutdown? I don't see how this situation is special in that regard.
    – marcelm
    Commented Sep 11, 2022 at 19:37

2 Answers 2

5

Create two systemd units and one script.

First unit:

/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

runs shutdown-ifno-logged-users.service once after 15min from the boot.

Second unit:

/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

starts your script to check logged users.

In your script parse output of loginctl --no-pager list-users or maybe even who -q and shutdown if there is no user logged.

If you want to prevent reboot once somebody logged in and then logged out before timer is triggered you may try to stop are-users-logged.timer from /etc/profile.d/somescript.sh with something like sudo systemctl stop are-users-logged.timer. But i guess it is maybe problematic if user is not root due password asking.

Better create some file in /tmp with echo from /etc/profile.d/somescript.sh to indicate somebody was logged and check in your script_to_check_logged_users if it exists and skip shutdown.

4

I want it to time out after 5 or 10 minutes of no login and shutdown.

Maybe grub timeout feature satisfy you.

Add custom menuentry with halt command into the grub configuration and make it default. Set grub timeout to needed value. After poweron or reboot grub selects your default entry and turn off the computer after timeout if there is no user activity,

To boot any OS you need to select and boot menu entry manually.

For Arch add into /etc/grub.d/40_custom

menuentry "System shutdown" {
   echo "System shutting down..."
   halt
}

To change the default selected entry, edit /etc/default/grub and change the value of GRUB_DEFAULT:

GRUB_DEFAULT='System shutdown'

then

sudo grub-mkconfig -o /boot/grub/grub.cfg
1
  • Nice idea, I think this could work in a Grub environment. I am however using systemd-boot and don't want to use Grub. The shutdown should be triggered while the pc is waiting at the tty-login.
    – Betaminos
    Commented Sep 11, 2022 at 15:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .