How to shut down linux server after running for 60 minutes

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
8
down vote

favorite
1












I have a server that it is normally switched off for security reasons. When I want to work on it I switch it on, execute my tasks, then shutdown it again. My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.



I've researched how to do this with cron, but I don't think it's the proper way because cron doesn't take into account when the server was last turned on, I can only set periodic patterns but they don't take that data into account.



How could I do this implementation?










share|improve this question









New contributor




jmhostalet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2




    Have a look at at (one-time execution).
    – dirkt
    13 hours ago






  • 3




    I haven't done this, but your login script could start a sudo shutdown -h +60 which would start a countdown counter of 60 mins for the shutdown (-halt) process. If you wanted to cancel it, you could sudo shutdown -c (this doesn't use cron though)
    – guiverc
    13 hours ago










  • I might have a solution that will shut it down as soon as your task is done. May I know how you run these tasks, by a bash script?
    – sdkks
    11 hours ago






  • 5




    Depending on the nature of your tasks maybe it's worth running them inside a docker container, so you don't need to worry about switching off a server. The container is created, it runs your tasks, and it's destroyed after that.
    – Vicente Olivert Riera
    11 hours ago















up vote
8
down vote

favorite
1












I have a server that it is normally switched off for security reasons. When I want to work on it I switch it on, execute my tasks, then shutdown it again. My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.



I've researched how to do this with cron, but I don't think it's the proper way because cron doesn't take into account when the server was last turned on, I can only set periodic patterns but they don't take that data into account.



How could I do this implementation?










share|improve this question









New contributor




jmhostalet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2




    Have a look at at (one-time execution).
    – dirkt
    13 hours ago






  • 3




    I haven't done this, but your login script could start a sudo shutdown -h +60 which would start a countdown counter of 60 mins for the shutdown (-halt) process. If you wanted to cancel it, you could sudo shutdown -c (this doesn't use cron though)
    – guiverc
    13 hours ago










  • I might have a solution that will shut it down as soon as your task is done. May I know how you run these tasks, by a bash script?
    – sdkks
    11 hours ago






  • 5




    Depending on the nature of your tasks maybe it's worth running them inside a docker container, so you don't need to worry about switching off a server. The container is created, it runs your tasks, and it's destroyed after that.
    – Vicente Olivert Riera
    11 hours ago













up vote
8
down vote

favorite
1









up vote
8
down vote

favorite
1






1





I have a server that it is normally switched off for security reasons. When I want to work on it I switch it on, execute my tasks, then shutdown it again. My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.



I've researched how to do this with cron, but I don't think it's the proper way because cron doesn't take into account when the server was last turned on, I can only set periodic patterns but they don't take that data into account.



How could I do this implementation?










share|improve this question









New contributor




jmhostalet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have a server that it is normally switched off for security reasons. When I want to work on it I switch it on, execute my tasks, then shutdown it again. My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.



I've researched how to do this with cron, but I don't think it's the proper way because cron doesn't take into account when the server was last turned on, I can only set periodic patterns but they don't take that data into account.



How could I do this implementation?







linux cron date shutdown






share|improve this question









New contributor




jmhostalet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




jmhostalet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 13 mins ago









Malandy

1032




1032






New contributor




jmhostalet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 13 hours ago









jmhostalet

1464




1464




New contributor




jmhostalet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





jmhostalet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






jmhostalet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 2




    Have a look at at (one-time execution).
    – dirkt
    13 hours ago






  • 3




    I haven't done this, but your login script could start a sudo shutdown -h +60 which would start a countdown counter of 60 mins for the shutdown (-halt) process. If you wanted to cancel it, you could sudo shutdown -c (this doesn't use cron though)
    – guiverc
    13 hours ago










  • I might have a solution that will shut it down as soon as your task is done. May I know how you run these tasks, by a bash script?
    – sdkks
    11 hours ago






  • 5




    Depending on the nature of your tasks maybe it's worth running them inside a docker container, so you don't need to worry about switching off a server. The container is created, it runs your tasks, and it's destroyed after that.
    – Vicente Olivert Riera
    11 hours ago













  • 2




    Have a look at at (one-time execution).
    – dirkt
    13 hours ago






  • 3




    I haven't done this, but your login script could start a sudo shutdown -h +60 which would start a countdown counter of 60 mins for the shutdown (-halt) process. If you wanted to cancel it, you could sudo shutdown -c (this doesn't use cron though)
    – guiverc
    13 hours ago










  • I might have a solution that will shut it down as soon as your task is done. May I know how you run these tasks, by a bash script?
    – sdkks
    11 hours ago






  • 5




    Depending on the nature of your tasks maybe it's worth running them inside a docker container, so you don't need to worry about switching off a server. The container is created, it runs your tasks, and it's destroyed after that.
    – Vicente Olivert Riera
    11 hours ago








2




2




Have a look at at (one-time execution).
– dirkt
13 hours ago




Have a look at at (one-time execution).
– dirkt
13 hours ago




3




3




I haven't done this, but your login script could start a sudo shutdown -h +60 which would start a countdown counter of 60 mins for the shutdown (-halt) process. If you wanted to cancel it, you could sudo shutdown -c (this doesn't use cron though)
– guiverc
13 hours ago




I haven't done this, but your login script could start a sudo shutdown -h +60 which would start a countdown counter of 60 mins for the shutdown (-halt) process. If you wanted to cancel it, you could sudo shutdown -c (this doesn't use cron though)
– guiverc
13 hours ago












I might have a solution that will shut it down as soon as your task is done. May I know how you run these tasks, by a bash script?
– sdkks
11 hours ago




I might have a solution that will shut it down as soon as your task is done. May I know how you run these tasks, by a bash script?
– sdkks
11 hours ago




5




5




Depending on the nature of your tasks maybe it's worth running them inside a docker container, so you don't need to worry about switching off a server. The container is created, it runs your tasks, and it's destroyed after that.
– Vicente Olivert Riera
11 hours ago





Depending on the nature of your tasks maybe it's worth running them inside a docker container, so you don't need to worry about switching off a server. The container is created, it runs your tasks, and it's destroyed after that.
– Vicente Olivert Riera
11 hours ago











7 Answers
7






active

oldest

votes

















up vote
12
down vote



accepted










If you execute your tasks as the same user every time, you can simply add the shutdown command with the option -P to your profile. The number stands for the amount of seconds the shutdown command is delayed.



echo "sudo shutdown -P 3600" >> ~/.profile





share|improve this answer
















  • 2




    The time is in minutes, not seconds. Try shutdown -k -P 3600 vs shutdown -k -P 60 (-k prints wall message but doesn't have other effect)
    – sebasth
    11 hours ago







  • 5




    But try the command one time first, you don't want an instant shutdown to be bound to your profile!
    – Fabian Röling
    10 hours ago






  • 6




    I imagine every ssh login would cause a new call of shutdown. Would the counter reset then?
    – JoL
    8 hours ago






  • 1




    It might also be worthwhile to mention that logins will not be allowed in the last 5 minutes, as mentioned in the shutdown manpage.
    – JoL
    8 hours ago

















up vote
32
down vote













There are a several options.




  • Provide time directly to shutdown -P:



    shutdown -P 60


    Note that shutdown man page also points out:




    If the time argument is used, 5 minutes before the system goes down the /run/nologin file is created to ensure that further logins shall not be allowed.




  • Use at command.


  • Create a systemd unit file or init script which runs shutdown -P 60 at startup.



  • Use cron's @reboot to run command after boot.



    Add to (root) crontab:



    @reboot shutdown -P 60


For the last two methods, you could also use sleep 3600 && shutdown -P instead of using time argument to shutdown to delay the shutdown for 60 minutes. This way logins are possible to the last moment before shutdown is issued.






share|improve this answer





























    up vote
    6
    down vote













    Hello and welcome to this site!



    Elaborating on @dirkt comment, you can insert an at command on your .bashrc or .profile or whichever file your shell uses on login to schedule an automatic shutdown 60 minutes after your login.



    Something like:



    at now + 60 minutes -f /sbin/halt





    share|improve this answer




















    • This task is right up at’s alley. You can also see at tasks in /var/spool/
      – sdkks
      11 hours ago






    • 2




      I would advice against using at in this context because it survives reboots. If OP were to log in, manually shutdown and then log in again in the span of an hour, he'd get kicked off before an hour passed after his last login when the first scheduled shutdown proc.
      – Aaron
      10 hours ago










    • @Aaron that’s true. Haven’t thought of that. Boot persistence is a caveat for this solution.
      – sdkks
      10 hours ago

















    up vote
    4
    down vote













    This looks like an XY problem.




    My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.




    If you shutdown after 60 minutes, you run the risk that you may be running a particularly complicated problem and just need to have more time. Many of the previous solutions would not make it easy to delay the shutdown.



    If the task is not an interactive task but instead are a scripted task that is automatically triggered from another machine, @sdkks gave a great solution for that; you really should just task the machine to run poweroff as soon as the script and all its tasks finishes.



    However, if your task is an interactive task, I'd suggest is to do idle detection instead.



    If you do your task in a GUI (X11) you can detect the idle GUI sessions using the approach described here: Run a command when system is idle and when is active again



    If you do the task through terminal, you can detect logged in user using the who command. You can setup a cronjob that shuts the machine down if who returns empty result. Note that this will be quite conservative approach, it will not shutdown the system if you left the console connected but idle.



    If you want to be a bit more aggressive and disconnect idle terminal session as well, you can combine the previous approach with automatically disconnecting idle SSH session ClientAliveInterval and ClientAliveCountMax. Another approach for this if you don't have SSH but have a local terminal session is to use the terminal IDLE time as returned by w command.






    share|improve this answer




















    • This is a very important point. Whatever you implement - it's in your interest to make sure that you can also cancel the shut down.
      – Shadow
      23 mins ago

















    up vote
    3
    down vote













    First of all, welcome.



    Although present answers here all satisfy the requirement to the perfection, I have a humble unsolicited suggestion, that's if you'd like your machine to power off as soon as the tasks are done.



    bash scripts can be trapped, meaning certain signals can be intercepted and certain tasks can be executed when needed. EXIT is one of the signals that can be trapped.



    You would be able to:



    1. Set a trap for EXIT of your automated shell scripts, meaning
      termination of your automated tasks

    2. Set a trap for your .bashrc EXIT, meaning whenever you log out
      of that machine, power it off.

    Option #1 would be the ideal case, provided your tasks don't require adhoc inspection and manual judgement.



    Option #2 would cover the cases where you'd forget to exit the terminal without powering off. There is a caveat though, if you have multiple terminals to same machine open and you exit from one of them, it will still power off the machine all the same. (It can be scripted to avoid that, but I won't complicate the solution.)



    cleanup()
    # Do some tasks before terminating
    echo oh la lah, cleaning is so nice
    echo "Cy'a later world"
    sudo poweroff & # finally shutdown

    trap cleanup EXIT


    This can be at the end of .bashrc for Option #2, at somewhere top of your script for Option #1.



    Why not use poweroff at the end of the script?



    I prefer to use set -eo pipefail in on top of my scripts, if any error happens, it won't silently fail, it will stop executing more commands. trap of EXIT signal should cover the cases where script terminates prematurely due to errors.



    However for your tasks, this might also mean machine will shutdown before they are completed.



    I have a simple bash template I use for making scripting easier to debug, maybe might be of some use. Please see this gist.






    share|improve this answer



























      up vote
      2
      down vote













      Try to put the script (sudo shutdown -P 3600) in the /etc/init.d directory to run it automatically at startup.
      Or try using anacron, adding the command in the /etc/anacrontab file.
      I also suggest to use nohup in front of the command to be sure that the command still working after logout






      share|improve this answer










      New contributor




      Saveriofr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.

















      • Depending on distro, it might not get executed by just being in init.d
        – sdkks
        11 hours ago











      • @sdkks, you're right. Anacron can be also considered. putting the script in /etc/anacrontab, also used with 'nohup' in the event of logout.
        – Saveriofr
        11 hours ago










      • @Saveriofr You can edit your answer to improve its quality (better than posting additional info as a comment).
        – Anthony Geoghegan
        11 hours ago

















      up vote
      1
      down vote













      If you're really concerned about security, use an mechanical outlet timer on the power source. Just set it to whatever time you want it to shutdown. This way no one can login remotely and disable the shutdown. You would need physical access.






      share|improve this answer




















        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
        );



        );






        jmhostalet is a new contributor. Be nice, and check out our Code of Conduct.









         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f472718%2fhow-to-shut-down-linux-server-after-running-for-60-minutes%23new-answer', 'question_page');

        );

        Post as a guest






























        7 Answers
        7






        active

        oldest

        votes








        7 Answers
        7






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        12
        down vote



        accepted










        If you execute your tasks as the same user every time, you can simply add the shutdown command with the option -P to your profile. The number stands for the amount of seconds the shutdown command is delayed.



        echo "sudo shutdown -P 3600" >> ~/.profile





        share|improve this answer
















        • 2




          The time is in minutes, not seconds. Try shutdown -k -P 3600 vs shutdown -k -P 60 (-k prints wall message but doesn't have other effect)
          – sebasth
          11 hours ago







        • 5




          But try the command one time first, you don't want an instant shutdown to be bound to your profile!
          – Fabian Röling
          10 hours ago






        • 6




          I imagine every ssh login would cause a new call of shutdown. Would the counter reset then?
          – JoL
          8 hours ago






        • 1




          It might also be worthwhile to mention that logins will not be allowed in the last 5 minutes, as mentioned in the shutdown manpage.
          – JoL
          8 hours ago














        up vote
        12
        down vote



        accepted










        If you execute your tasks as the same user every time, you can simply add the shutdown command with the option -P to your profile. The number stands for the amount of seconds the shutdown command is delayed.



        echo "sudo shutdown -P 3600" >> ~/.profile





        share|improve this answer
















        • 2




          The time is in minutes, not seconds. Try shutdown -k -P 3600 vs shutdown -k -P 60 (-k prints wall message but doesn't have other effect)
          – sebasth
          11 hours ago







        • 5




          But try the command one time first, you don't want an instant shutdown to be bound to your profile!
          – Fabian Röling
          10 hours ago






        • 6




          I imagine every ssh login would cause a new call of shutdown. Would the counter reset then?
          – JoL
          8 hours ago






        • 1




          It might also be worthwhile to mention that logins will not be allowed in the last 5 minutes, as mentioned in the shutdown manpage.
          – JoL
          8 hours ago












        up vote
        12
        down vote



        accepted







        up vote
        12
        down vote



        accepted






        If you execute your tasks as the same user every time, you can simply add the shutdown command with the option -P to your profile. The number stands for the amount of seconds the shutdown command is delayed.



        echo "sudo shutdown -P 3600" >> ~/.profile





        share|improve this answer












        If you execute your tasks as the same user every time, you can simply add the shutdown command with the option -P to your profile. The number stands for the amount of seconds the shutdown command is delayed.



        echo "sudo shutdown -P 3600" >> ~/.profile






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 13 hours ago









        Dirk Krijgsman

        1844




        1844







        • 2




          The time is in minutes, not seconds. Try shutdown -k -P 3600 vs shutdown -k -P 60 (-k prints wall message but doesn't have other effect)
          – sebasth
          11 hours ago







        • 5




          But try the command one time first, you don't want an instant shutdown to be bound to your profile!
          – Fabian Röling
          10 hours ago






        • 6




          I imagine every ssh login would cause a new call of shutdown. Would the counter reset then?
          – JoL
          8 hours ago






        • 1




          It might also be worthwhile to mention that logins will not be allowed in the last 5 minutes, as mentioned in the shutdown manpage.
          – JoL
          8 hours ago












        • 2




          The time is in minutes, not seconds. Try shutdown -k -P 3600 vs shutdown -k -P 60 (-k prints wall message but doesn't have other effect)
          – sebasth
          11 hours ago







        • 5




          But try the command one time first, you don't want an instant shutdown to be bound to your profile!
          – Fabian Röling
          10 hours ago






        • 6




          I imagine every ssh login would cause a new call of shutdown. Would the counter reset then?
          – JoL
          8 hours ago






        • 1




          It might also be worthwhile to mention that logins will not be allowed in the last 5 minutes, as mentioned in the shutdown manpage.
          – JoL
          8 hours ago







        2




        2




        The time is in minutes, not seconds. Try shutdown -k -P 3600 vs shutdown -k -P 60 (-k prints wall message but doesn't have other effect)
        – sebasth
        11 hours ago





        The time is in minutes, not seconds. Try shutdown -k -P 3600 vs shutdown -k -P 60 (-k prints wall message but doesn't have other effect)
        – sebasth
        11 hours ago





        5




        5




        But try the command one time first, you don't want an instant shutdown to be bound to your profile!
        – Fabian Röling
        10 hours ago




        But try the command one time first, you don't want an instant shutdown to be bound to your profile!
        – Fabian Röling
        10 hours ago




        6




        6




        I imagine every ssh login would cause a new call of shutdown. Would the counter reset then?
        – JoL
        8 hours ago




        I imagine every ssh login would cause a new call of shutdown. Would the counter reset then?
        – JoL
        8 hours ago




        1




        1




        It might also be worthwhile to mention that logins will not be allowed in the last 5 minutes, as mentioned in the shutdown manpage.
        – JoL
        8 hours ago




        It might also be worthwhile to mention that logins will not be allowed in the last 5 minutes, as mentioned in the shutdown manpage.
        – JoL
        8 hours ago












        up vote
        32
        down vote













        There are a several options.




        • Provide time directly to shutdown -P:



          shutdown -P 60


          Note that shutdown man page also points out:




          If the time argument is used, 5 minutes before the system goes down the /run/nologin file is created to ensure that further logins shall not be allowed.




        • Use at command.


        • Create a systemd unit file or init script which runs shutdown -P 60 at startup.



        • Use cron's @reboot to run command after boot.



          Add to (root) crontab:



          @reboot shutdown -P 60


        For the last two methods, you could also use sleep 3600 && shutdown -P instead of using time argument to shutdown to delay the shutdown for 60 minutes. This way logins are possible to the last moment before shutdown is issued.






        share|improve this answer


























          up vote
          32
          down vote













          There are a several options.




          • Provide time directly to shutdown -P:



            shutdown -P 60


            Note that shutdown man page also points out:




            If the time argument is used, 5 minutes before the system goes down the /run/nologin file is created to ensure that further logins shall not be allowed.




          • Use at command.


          • Create a systemd unit file or init script which runs shutdown -P 60 at startup.



          • Use cron's @reboot to run command after boot.



            Add to (root) crontab:



            @reboot shutdown -P 60


          For the last two methods, you could also use sleep 3600 && shutdown -P instead of using time argument to shutdown to delay the shutdown for 60 minutes. This way logins are possible to the last moment before shutdown is issued.






          share|improve this answer
























            up vote
            32
            down vote










            up vote
            32
            down vote









            There are a several options.




            • Provide time directly to shutdown -P:



              shutdown -P 60


              Note that shutdown man page also points out:




              If the time argument is used, 5 minutes before the system goes down the /run/nologin file is created to ensure that further logins shall not be allowed.




            • Use at command.


            • Create a systemd unit file or init script which runs shutdown -P 60 at startup.



            • Use cron's @reboot to run command after boot.



              Add to (root) crontab:



              @reboot shutdown -P 60


            For the last two methods, you could also use sleep 3600 && shutdown -P instead of using time argument to shutdown to delay the shutdown for 60 minutes. This way logins are possible to the last moment before shutdown is issued.






            share|improve this answer














            There are a several options.




            • Provide time directly to shutdown -P:



              shutdown -P 60


              Note that shutdown man page also points out:




              If the time argument is used, 5 minutes before the system goes down the /run/nologin file is created to ensure that further logins shall not be allowed.




            • Use at command.


            • Create a systemd unit file or init script which runs shutdown -P 60 at startup.



            • Use cron's @reboot to run command after boot.



              Add to (root) crontab:



              @reboot shutdown -P 60


            For the last two methods, you could also use sleep 3600 && shutdown -P instead of using time argument to shutdown to delay the shutdown for 60 minutes. This way logins are possible to the last moment before shutdown is issued.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 7 hours ago

























            answered 13 hours ago









            sebasth

            6,80131745




            6,80131745




















                up vote
                6
                down vote













                Hello and welcome to this site!



                Elaborating on @dirkt comment, you can insert an at command on your .bashrc or .profile or whichever file your shell uses on login to schedule an automatic shutdown 60 minutes after your login.



                Something like:



                at now + 60 minutes -f /sbin/halt





                share|improve this answer




















                • This task is right up at’s alley. You can also see at tasks in /var/spool/
                  – sdkks
                  11 hours ago






                • 2




                  I would advice against using at in this context because it survives reboots. If OP were to log in, manually shutdown and then log in again in the span of an hour, he'd get kicked off before an hour passed after his last login when the first scheduled shutdown proc.
                  – Aaron
                  10 hours ago










                • @Aaron that’s true. Haven’t thought of that. Boot persistence is a caveat for this solution.
                  – sdkks
                  10 hours ago














                up vote
                6
                down vote













                Hello and welcome to this site!



                Elaborating on @dirkt comment, you can insert an at command on your .bashrc or .profile or whichever file your shell uses on login to schedule an automatic shutdown 60 minutes after your login.



                Something like:



                at now + 60 minutes -f /sbin/halt





                share|improve this answer




















                • This task is right up at’s alley. You can also see at tasks in /var/spool/
                  – sdkks
                  11 hours ago






                • 2




                  I would advice against using at in this context because it survives reboots. If OP were to log in, manually shutdown and then log in again in the span of an hour, he'd get kicked off before an hour passed after his last login when the first scheduled shutdown proc.
                  – Aaron
                  10 hours ago










                • @Aaron that’s true. Haven’t thought of that. Boot persistence is a caveat for this solution.
                  – sdkks
                  10 hours ago












                up vote
                6
                down vote










                up vote
                6
                down vote









                Hello and welcome to this site!



                Elaborating on @dirkt comment, you can insert an at command on your .bashrc or .profile or whichever file your shell uses on login to schedule an automatic shutdown 60 minutes after your login.



                Something like:



                at now + 60 minutes -f /sbin/halt





                share|improve this answer












                Hello and welcome to this site!



                Elaborating on @dirkt comment, you can insert an at command on your .bashrc or .profile or whichever file your shell uses on login to schedule an automatic shutdown 60 minutes after your login.



                Something like:



                at now + 60 minutes -f /sbin/halt






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 13 hours ago









                Mr Shunz

                2,76811720




                2,76811720











                • This task is right up at’s alley. You can also see at tasks in /var/spool/
                  – sdkks
                  11 hours ago






                • 2




                  I would advice against using at in this context because it survives reboots. If OP were to log in, manually shutdown and then log in again in the span of an hour, he'd get kicked off before an hour passed after his last login when the first scheduled shutdown proc.
                  – Aaron
                  10 hours ago










                • @Aaron that’s true. Haven’t thought of that. Boot persistence is a caveat for this solution.
                  – sdkks
                  10 hours ago
















                • This task is right up at’s alley. You can also see at tasks in /var/spool/
                  – sdkks
                  11 hours ago






                • 2




                  I would advice against using at in this context because it survives reboots. If OP were to log in, manually shutdown and then log in again in the span of an hour, he'd get kicked off before an hour passed after his last login when the first scheduled shutdown proc.
                  – Aaron
                  10 hours ago










                • @Aaron that’s true. Haven’t thought of that. Boot persistence is a caveat for this solution.
                  – sdkks
                  10 hours ago















                This task is right up at’s alley. You can also see at tasks in /var/spool/
                – sdkks
                11 hours ago




                This task is right up at’s alley. You can also see at tasks in /var/spool/
                – sdkks
                11 hours ago




                2




                2




                I would advice against using at in this context because it survives reboots. If OP were to log in, manually shutdown and then log in again in the span of an hour, he'd get kicked off before an hour passed after his last login when the first scheduled shutdown proc.
                – Aaron
                10 hours ago




                I would advice against using at in this context because it survives reboots. If OP were to log in, manually shutdown and then log in again in the span of an hour, he'd get kicked off before an hour passed after his last login when the first scheduled shutdown proc.
                – Aaron
                10 hours ago












                @Aaron that’s true. Haven’t thought of that. Boot persistence is a caveat for this solution.
                – sdkks
                10 hours ago




                @Aaron that’s true. Haven’t thought of that. Boot persistence is a caveat for this solution.
                – sdkks
                10 hours ago










                up vote
                4
                down vote













                This looks like an XY problem.




                My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.




                If you shutdown after 60 minutes, you run the risk that you may be running a particularly complicated problem and just need to have more time. Many of the previous solutions would not make it easy to delay the shutdown.



                If the task is not an interactive task but instead are a scripted task that is automatically triggered from another machine, @sdkks gave a great solution for that; you really should just task the machine to run poweroff as soon as the script and all its tasks finishes.



                However, if your task is an interactive task, I'd suggest is to do idle detection instead.



                If you do your task in a GUI (X11) you can detect the idle GUI sessions using the approach described here: Run a command when system is idle and when is active again



                If you do the task through terminal, you can detect logged in user using the who command. You can setup a cronjob that shuts the machine down if who returns empty result. Note that this will be quite conservative approach, it will not shutdown the system if you left the console connected but idle.



                If you want to be a bit more aggressive and disconnect idle terminal session as well, you can combine the previous approach with automatically disconnecting idle SSH session ClientAliveInterval and ClientAliveCountMax. Another approach for this if you don't have SSH but have a local terminal session is to use the terminal IDLE time as returned by w command.






                share|improve this answer




















                • This is a very important point. Whatever you implement - it's in your interest to make sure that you can also cancel the shut down.
                  – Shadow
                  23 mins ago














                up vote
                4
                down vote













                This looks like an XY problem.




                My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.




                If you shutdown after 60 minutes, you run the risk that you may be running a particularly complicated problem and just need to have more time. Many of the previous solutions would not make it easy to delay the shutdown.



                If the task is not an interactive task but instead are a scripted task that is automatically triggered from another machine, @sdkks gave a great solution for that; you really should just task the machine to run poweroff as soon as the script and all its tasks finishes.



                However, if your task is an interactive task, I'd suggest is to do idle detection instead.



                If you do your task in a GUI (X11) you can detect the idle GUI sessions using the approach described here: Run a command when system is idle and when is active again



                If you do the task through terminal, you can detect logged in user using the who command. You can setup a cronjob that shuts the machine down if who returns empty result. Note that this will be quite conservative approach, it will not shutdown the system if you left the console connected but idle.



                If you want to be a bit more aggressive and disconnect idle terminal session as well, you can combine the previous approach with automatically disconnecting idle SSH session ClientAliveInterval and ClientAliveCountMax. Another approach for this if you don't have SSH but have a local terminal session is to use the terminal IDLE time as returned by w command.






                share|improve this answer




















                • This is a very important point. Whatever you implement - it's in your interest to make sure that you can also cancel the shut down.
                  – Shadow
                  23 mins ago












                up vote
                4
                down vote










                up vote
                4
                down vote









                This looks like an XY problem.




                My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.




                If you shutdown after 60 minutes, you run the risk that you may be running a particularly complicated problem and just need to have more time. Many of the previous solutions would not make it easy to delay the shutdown.



                If the task is not an interactive task but instead are a scripted task that is automatically triggered from another machine, @sdkks gave a great solution for that; you really should just task the machine to run poweroff as soon as the script and all its tasks finishes.



                However, if your task is an interactive task, I'd suggest is to do idle detection instead.



                If you do your task in a GUI (X11) you can detect the idle GUI sessions using the approach described here: Run a command when system is idle and when is active again



                If you do the task through terminal, you can detect logged in user using the who command. You can setup a cronjob that shuts the machine down if who returns empty result. Note that this will be quite conservative approach, it will not shutdown the system if you left the console connected but idle.



                If you want to be a bit more aggressive and disconnect idle terminal session as well, you can combine the previous approach with automatically disconnecting idle SSH session ClientAliveInterval and ClientAliveCountMax. Another approach for this if you don't have SSH but have a local terminal session is to use the terminal IDLE time as returned by w command.






                share|improve this answer












                This looks like an XY problem.




                My tasks take usually no more than 15 minutes. I would like to implement a mechanism to shutdown it automatically after 60 minutes.




                If you shutdown after 60 minutes, you run the risk that you may be running a particularly complicated problem and just need to have more time. Many of the previous solutions would not make it easy to delay the shutdown.



                If the task is not an interactive task but instead are a scripted task that is automatically triggered from another machine, @sdkks gave a great solution for that; you really should just task the machine to run poweroff as soon as the script and all its tasks finishes.



                However, if your task is an interactive task, I'd suggest is to do idle detection instead.



                If you do your task in a GUI (X11) you can detect the idle GUI sessions using the approach described here: Run a command when system is idle and when is active again



                If you do the task through terminal, you can detect logged in user using the who command. You can setup a cronjob that shuts the machine down if who returns empty result. Note that this will be quite conservative approach, it will not shutdown the system if you left the console connected but idle.



                If you want to be a bit more aggressive and disconnect idle terminal session as well, you can combine the previous approach with automatically disconnecting idle SSH session ClientAliveInterval and ClientAliveCountMax. Another approach for this if you don't have SSH but have a local terminal session is to use the terminal IDLE time as returned by w command.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 8 hours ago









                Lie Ryan

                66337




                66337











                • This is a very important point. Whatever you implement - it's in your interest to make sure that you can also cancel the shut down.
                  – Shadow
                  23 mins ago
















                • This is a very important point. Whatever you implement - it's in your interest to make sure that you can also cancel the shut down.
                  – Shadow
                  23 mins ago















                This is a very important point. Whatever you implement - it's in your interest to make sure that you can also cancel the shut down.
                – Shadow
                23 mins ago




                This is a very important point. Whatever you implement - it's in your interest to make sure that you can also cancel the shut down.
                – Shadow
                23 mins ago










                up vote
                3
                down vote













                First of all, welcome.



                Although present answers here all satisfy the requirement to the perfection, I have a humble unsolicited suggestion, that's if you'd like your machine to power off as soon as the tasks are done.



                bash scripts can be trapped, meaning certain signals can be intercepted and certain tasks can be executed when needed. EXIT is one of the signals that can be trapped.



                You would be able to:



                1. Set a trap for EXIT of your automated shell scripts, meaning
                  termination of your automated tasks

                2. Set a trap for your .bashrc EXIT, meaning whenever you log out
                  of that machine, power it off.

                Option #1 would be the ideal case, provided your tasks don't require adhoc inspection and manual judgement.



                Option #2 would cover the cases where you'd forget to exit the terminal without powering off. There is a caveat though, if you have multiple terminals to same machine open and you exit from one of them, it will still power off the machine all the same. (It can be scripted to avoid that, but I won't complicate the solution.)



                cleanup()
                # Do some tasks before terminating
                echo oh la lah, cleaning is so nice
                echo "Cy'a later world"
                sudo poweroff & # finally shutdown

                trap cleanup EXIT


                This can be at the end of .bashrc for Option #2, at somewhere top of your script for Option #1.



                Why not use poweroff at the end of the script?



                I prefer to use set -eo pipefail in on top of my scripts, if any error happens, it won't silently fail, it will stop executing more commands. trap of EXIT signal should cover the cases where script terminates prematurely due to errors.



                However for your tasks, this might also mean machine will shutdown before they are completed.



                I have a simple bash template I use for making scripting easier to debug, maybe might be of some use. Please see this gist.






                share|improve this answer
























                  up vote
                  3
                  down vote













                  First of all, welcome.



                  Although present answers here all satisfy the requirement to the perfection, I have a humble unsolicited suggestion, that's if you'd like your machine to power off as soon as the tasks are done.



                  bash scripts can be trapped, meaning certain signals can be intercepted and certain tasks can be executed when needed. EXIT is one of the signals that can be trapped.



                  You would be able to:



                  1. Set a trap for EXIT of your automated shell scripts, meaning
                    termination of your automated tasks

                  2. Set a trap for your .bashrc EXIT, meaning whenever you log out
                    of that machine, power it off.

                  Option #1 would be the ideal case, provided your tasks don't require adhoc inspection and manual judgement.



                  Option #2 would cover the cases where you'd forget to exit the terminal without powering off. There is a caveat though, if you have multiple terminals to same machine open and you exit from one of them, it will still power off the machine all the same. (It can be scripted to avoid that, but I won't complicate the solution.)



                  cleanup()
                  # Do some tasks before terminating
                  echo oh la lah, cleaning is so nice
                  echo "Cy'a later world"
                  sudo poweroff & # finally shutdown

                  trap cleanup EXIT


                  This can be at the end of .bashrc for Option #2, at somewhere top of your script for Option #1.



                  Why not use poweroff at the end of the script?



                  I prefer to use set -eo pipefail in on top of my scripts, if any error happens, it won't silently fail, it will stop executing more commands. trap of EXIT signal should cover the cases where script terminates prematurely due to errors.



                  However for your tasks, this might also mean machine will shutdown before they are completed.



                  I have a simple bash template I use for making scripting easier to debug, maybe might be of some use. Please see this gist.






                  share|improve this answer






















                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    First of all, welcome.



                    Although present answers here all satisfy the requirement to the perfection, I have a humble unsolicited suggestion, that's if you'd like your machine to power off as soon as the tasks are done.



                    bash scripts can be trapped, meaning certain signals can be intercepted and certain tasks can be executed when needed. EXIT is one of the signals that can be trapped.



                    You would be able to:



                    1. Set a trap for EXIT of your automated shell scripts, meaning
                      termination of your automated tasks

                    2. Set a trap for your .bashrc EXIT, meaning whenever you log out
                      of that machine, power it off.

                    Option #1 would be the ideal case, provided your tasks don't require adhoc inspection and manual judgement.



                    Option #2 would cover the cases where you'd forget to exit the terminal without powering off. There is a caveat though, if you have multiple terminals to same machine open and you exit from one of them, it will still power off the machine all the same. (It can be scripted to avoid that, but I won't complicate the solution.)



                    cleanup()
                    # Do some tasks before terminating
                    echo oh la lah, cleaning is so nice
                    echo "Cy'a later world"
                    sudo poweroff & # finally shutdown

                    trap cleanup EXIT


                    This can be at the end of .bashrc for Option #2, at somewhere top of your script for Option #1.



                    Why not use poweroff at the end of the script?



                    I prefer to use set -eo pipefail in on top of my scripts, if any error happens, it won't silently fail, it will stop executing more commands. trap of EXIT signal should cover the cases where script terminates prematurely due to errors.



                    However for your tasks, this might also mean machine will shutdown before they are completed.



                    I have a simple bash template I use for making scripting easier to debug, maybe might be of some use. Please see this gist.






                    share|improve this answer












                    First of all, welcome.



                    Although present answers here all satisfy the requirement to the perfection, I have a humble unsolicited suggestion, that's if you'd like your machine to power off as soon as the tasks are done.



                    bash scripts can be trapped, meaning certain signals can be intercepted and certain tasks can be executed when needed. EXIT is one of the signals that can be trapped.



                    You would be able to:



                    1. Set a trap for EXIT of your automated shell scripts, meaning
                      termination of your automated tasks

                    2. Set a trap for your .bashrc EXIT, meaning whenever you log out
                      of that machine, power it off.

                    Option #1 would be the ideal case, provided your tasks don't require adhoc inspection and manual judgement.



                    Option #2 would cover the cases where you'd forget to exit the terminal without powering off. There is a caveat though, if you have multiple terminals to same machine open and you exit from one of them, it will still power off the machine all the same. (It can be scripted to avoid that, but I won't complicate the solution.)



                    cleanup()
                    # Do some tasks before terminating
                    echo oh la lah, cleaning is so nice
                    echo "Cy'a later world"
                    sudo poweroff & # finally shutdown

                    trap cleanup EXIT


                    This can be at the end of .bashrc for Option #2, at somewhere top of your script for Option #1.



                    Why not use poweroff at the end of the script?



                    I prefer to use set -eo pipefail in on top of my scripts, if any error happens, it won't silently fail, it will stop executing more commands. trap of EXIT signal should cover the cases where script terminates prematurely due to errors.



                    However for your tasks, this might also mean machine will shutdown before they are completed.



                    I have a simple bash template I use for making scripting easier to debug, maybe might be of some use. Please see this gist.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 10 hours ago









                    sdkks

                    1836




                    1836




















                        up vote
                        2
                        down vote













                        Try to put the script (sudo shutdown -P 3600) in the /etc/init.d directory to run it automatically at startup.
                        Or try using anacron, adding the command in the /etc/anacrontab file.
                        I also suggest to use nohup in front of the command to be sure that the command still working after logout






                        share|improve this answer










                        New contributor




                        Saveriofr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.

















                        • Depending on distro, it might not get executed by just being in init.d
                          – sdkks
                          11 hours ago











                        • @sdkks, you're right. Anacron can be also considered. putting the script in /etc/anacrontab, also used with 'nohup' in the event of logout.
                          – Saveriofr
                          11 hours ago










                        • @Saveriofr You can edit your answer to improve its quality (better than posting additional info as a comment).
                          – Anthony Geoghegan
                          11 hours ago














                        up vote
                        2
                        down vote













                        Try to put the script (sudo shutdown -P 3600) in the /etc/init.d directory to run it automatically at startup.
                        Or try using anacron, adding the command in the /etc/anacrontab file.
                        I also suggest to use nohup in front of the command to be sure that the command still working after logout






                        share|improve this answer










                        New contributor




                        Saveriofr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.

















                        • Depending on distro, it might not get executed by just being in init.d
                          – sdkks
                          11 hours ago











                        • @sdkks, you're right. Anacron can be also considered. putting the script in /etc/anacrontab, also used with 'nohup' in the event of logout.
                          – Saveriofr
                          11 hours ago










                        • @Saveriofr You can edit your answer to improve its quality (better than posting additional info as a comment).
                          – Anthony Geoghegan
                          11 hours ago












                        up vote
                        2
                        down vote










                        up vote
                        2
                        down vote









                        Try to put the script (sudo shutdown -P 3600) in the /etc/init.d directory to run it automatically at startup.
                        Or try using anacron, adding the command in the /etc/anacrontab file.
                        I also suggest to use nohup in front of the command to be sure that the command still working after logout






                        share|improve this answer










                        New contributor




                        Saveriofr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.









                        Try to put the script (sudo shutdown -P 3600) in the /etc/init.d directory to run it automatically at startup.
                        Or try using anacron, adding the command in the /etc/anacrontab file.
                        I also suggest to use nohup in front of the command to be sure that the command still working after logout







                        share|improve this answer










                        New contributor




                        Saveriofr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.









                        share|improve this answer



                        share|improve this answer








                        edited 9 hours ago





















                        New contributor




                        Saveriofr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.









                        answered 12 hours ago









                        Saveriofr

                        173




                        173




                        New contributor




                        Saveriofr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.





                        New contributor





                        Saveriofr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.






                        Saveriofr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.











                        • Depending on distro, it might not get executed by just being in init.d
                          – sdkks
                          11 hours ago











                        • @sdkks, you're right. Anacron can be also considered. putting the script in /etc/anacrontab, also used with 'nohup' in the event of logout.
                          – Saveriofr
                          11 hours ago










                        • @Saveriofr You can edit your answer to improve its quality (better than posting additional info as a comment).
                          – Anthony Geoghegan
                          11 hours ago
















                        • Depending on distro, it might not get executed by just being in init.d
                          – sdkks
                          11 hours ago











                        • @sdkks, you're right. Anacron can be also considered. putting the script in /etc/anacrontab, also used with 'nohup' in the event of logout.
                          – Saveriofr
                          11 hours ago










                        • @Saveriofr You can edit your answer to improve its quality (better than posting additional info as a comment).
                          – Anthony Geoghegan
                          11 hours ago















                        Depending on distro, it might not get executed by just being in init.d
                        – sdkks
                        11 hours ago





                        Depending on distro, it might not get executed by just being in init.d
                        – sdkks
                        11 hours ago













                        @sdkks, you're right. Anacron can be also considered. putting the script in /etc/anacrontab, also used with 'nohup' in the event of logout.
                        – Saveriofr
                        11 hours ago




                        @sdkks, you're right. Anacron can be also considered. putting the script in /etc/anacrontab, also used with 'nohup' in the event of logout.
                        – Saveriofr
                        11 hours ago












                        @Saveriofr You can edit your answer to improve its quality (better than posting additional info as a comment).
                        – Anthony Geoghegan
                        11 hours ago




                        @Saveriofr You can edit your answer to improve its quality (better than posting additional info as a comment).
                        – Anthony Geoghegan
                        11 hours ago










                        up vote
                        1
                        down vote













                        If you're really concerned about security, use an mechanical outlet timer on the power source. Just set it to whatever time you want it to shutdown. This way no one can login remotely and disable the shutdown. You would need physical access.






                        share|improve this answer
























                          up vote
                          1
                          down vote













                          If you're really concerned about security, use an mechanical outlet timer on the power source. Just set it to whatever time you want it to shutdown. This way no one can login remotely and disable the shutdown. You would need physical access.






                          share|improve this answer






















                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            If you're really concerned about security, use an mechanical outlet timer on the power source. Just set it to whatever time you want it to shutdown. This way no one can login remotely and disable the shutdown. You would need physical access.






                            share|improve this answer












                            If you're really concerned about security, use an mechanical outlet timer on the power source. Just set it to whatever time you want it to shutdown. This way no one can login remotely and disable the shutdown. You would need physical access.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 6 hours ago









                            gogators

                            2671210




                            2671210




















                                jmhostalet is a new contributor. Be nice, and check out our Code of Conduct.









                                 

                                draft saved


                                draft discarded


















                                jmhostalet is a new contributor. Be nice, and check out our Code of Conduct.












                                jmhostalet is a new contributor. Be nice, and check out our Code of Conduct.











                                jmhostalet is a new contributor. Be nice, and check out our Code of Conduct.













                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f472718%2fhow-to-shut-down-linux-server-after-running-for-60-minutes%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                                Confectionery