Is there a command to reboot a machine whenever the next time that there are no current users logged in occur?

Currently, I have to SSH to machines and use the "w" command to see the users logged in so I know if its okay to reboot or not. If there is a user logged in, I simply can’t reboot because they might have unsaved work left on their Linux box, which is often the case. This can come very time consuming when I have to SSH to the same machine multiple times a week waiting for a time when there are no users logged in.

To make this really simple, I need the following.

A command that will reboot the computer when no users are currently logged in.


This should help... but it's not a command but short script. I believe there is no command for it. It's a script for use with cron, but you can execute it remotely on your machines ofcourse.
I'm using it for rebooting after updates.

#  Run this script as a cronjob at some suitable time in the PM, e.g. 10pm.
# Turns off workarea machine if no one is logged on, if someone is logged on
# then checks at 15 minute intervals until a set hour is reached.
# Must be used in conjuntion with the following BIOS settings:
# - Machine powers on in event of AC power being restored after loss
# - Machines turns itself on at a time at least 1 hour after the hour specified
# for $giveupat

# time to give up checking if anyone is logged on at. Specify as an hour on 
#24 clock  e.g. for 7am set to 07. for 7pm set to 19 (though you probably 
#do not want to specify a time in the evening!)

# while someone is logged in to the machine...
while [ -n "$(who)" ];do
   if [ "$(date +%H)" != "$giveupat" ];then
   # if time hasn't read the hour at which to give up, sleep 15 minutes
      sleep 900 
     # otherwise stop the script

# turn off gui login screen
service xdm stop

# reboot
reboot now

Source: linuxquestions.org


If I may be so bold to suggest another approach...

One of the cool things with being root is that you actually get to decide stuff! So, instead of waiting for users not to be logged in, I would set a fixed time for rebooting the system.

There are ample utilities such as wall:

       wall  displays  a  message, or the contents of a file, or otherwise its
       standard input, on the terminals of all currently logged in users. 

To notify logged in users that there will be a reboot, and you could give them enough time to save their work and log out. You can also use the shutdown command itself:

sudo shutdown -r +10
Shutdown scheduled for Fri 2018-01-05 19:21:46 CET

So, I would use something like this:



echo "Rebooting system shortly. Please save your work and log out" | wall

sleep 60

shutdown -r +10

Then run it (for example) the first day of every month at midnight with this crontab entry:

0 0 1 * * /usr/local/bin/reboot.sh >/dev/null 2>&1

You can of course name your script what you want, and place it where you like. The above is just an example.

  • Most Linux shutdowns have a wall message option built in.
    – Jeff Schaller
    Jan 5 '18 at 18:27
  • @JeffSchaller Yeah, I know. Just showing as an example. Plus if you are warned TWICE and still do not heed the warnings, I as a sysadmin feel that you really only have yourself to blame! Jan 5 '18 at 18:28
  • 1
    Sadly with the environment i work in, this will not do. No matter how many times i warn the user, if i shutdown their pc with unsaved work, i will be the one to blame. I appreciate the answer, i did learn from it.
    – TrevorKS
    Jan 5 '18 at 21:45

Nearly three years late. Found via Google.

For the one-liner fans. For sudo crontab -e:

26 */3 * * * [ -z "$(/usr/bin/who)" ] && /sbin/reboot

Reboot every third hour by 26 minutes if nobody is logged in (who returns an empty/zero string).


Find out your absolute path with which who or which reboot.

Tested on Ubuntu 16.04.6.

Now to your question.

[ "$(who | awk '{print $1}' | sort | uniq | wc -l)" -lt 2 ] && reboot

Reboot when less than two different users are logged in.

It is far from perfect but may help someone.

Tested on Ubuntu 20.04.1.

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.