Command to reboot machine when no users are logged in?

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question


























    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.







    share|improve this question
























      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.







      share|improve this question














      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.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 5 at 18:23









      Jeff Schaller

      31.8k848109




      31.8k848109










      asked Jan 5 at 17:08









      TrevorKS

      1188




      1188




















          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







          share|improve this answer





























            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.






            share|improve this answer






















            • 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











            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            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






























            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







            share|improve this answer


























              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







              share|improve this answer
























                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







                share|improve this answer














                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








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 5 at 17:56

























                answered Jan 5 at 17:49









                Petr

                642313




                642313






















                    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.






                    share|improve this answer






















                    • 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















                    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.






                    share|improve this answer






















                    • 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













                    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.






                    share|improve this answer














                    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.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    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

















                    • 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













                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    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













































































                    Popular posts from this blog

                    Peggy Mitchell

                    Palaiologos

                    The Forum (Inglewood, California)