Command to reboot machine when no users are logged in?

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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.
linux scripting users reboot
add a comment |Â
up vote
0
down vote
favorite
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.
linux scripting users reboot
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
linux scripting users reboot
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.
linux scripting users reboot
edited Jan 5 at 18:23
Jeff Schaller
31.8k848109
31.8k848109
asked Jan 5 at 17:08
TrevorKS
1188
1188
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
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
add a comment |Â
up vote
1
down vote
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 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 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 at 21:45
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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
add a comment |Â
up vote
0
down vote
accepted
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
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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
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
edited Jan 5 at 17:56
answered Jan 5 at 17:49
Petr
642313
642313
add a comment |Â
add a comment |Â
up vote
1
down vote
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 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 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 at 21:45
add a comment |Â
up vote
1
down vote
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 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 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 at 21:45
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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.
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.
edited Jan 5 at 18:30
answered Jan 5 at 18:12
maulinglawns
5,5082822
5,5082822
Most Linux shutdowns have a wall message option built in.
â Jeff Schaller
Jan 5 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 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 at 21:45
add a comment |Â
Most Linux shutdowns have a wall message option built in.
â Jeff Schaller
Jan 5 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 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 at 21:45
Most Linux shutdowns have a wall message option built in.
â Jeff Schaller
Jan 5 at 18:27
Most Linux shutdowns have a wall message option built in.
â Jeff Schaller
Jan 5 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 at 18:28
@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 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 at 21:45
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 at 21:45
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f415032%2fcommand-to-reboot-machine-when-no-users-are-logged-in%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password