0

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.

0

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.

#!/bin/bash
#  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!)
giveupat="07"



# 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 
   else
     # otherwise stop the script
      exit
   fi
done


# turn off gui login screen
service xdm stop


# reboot
reboot now

Source: linuxquestions.org

1

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:

DESCRIPTION
       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:

#!/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

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! – maulinglawns Jan 5 '18 at 18:28
  • 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

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.