I want to write a bash script that will run with cron every 5 minutes that will check if there's any active users logged in and if no active users are logged in, it will shutdown. I've come up with this:

if [ $l -lt 2 ]                                                                                                                        

and was wondering if this will do it or if users will return something unexpected after the last one has disconnected.

Additional information:

I realized something else: I just tried this out on an system at home running Arch and I got the following:

[ron@osboxes ~]$ users
[ron@osboxes ~]$ who
[ron@osboxes ~]$

how can this be? It's obvious that I'm an actively logged-on user, is it not?

  • 1
    write script that writes data to a log instead of shutting the server down ... then you can determine what users returns when the last user logs out – jsotola Jan 24 '20 at 19:39

It should be a comment but I don't have 50 rep point, so I will lose even more, post this comment as answer :)

If you run this script in every five min, your server could shutdown while booting in before you even able to login. So not a good idea. Write a systemd timer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.