Skip to main content
9 of 9
added 84 characters in body
gapsf
  • 624
  • 4
  • 7

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.

gapsf
  • 624
  • 4
  • 7