How do I shutdown the computer automatically, after bot finishes running from terminal?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















I would like to know how can I schedule a shutdown in my computer after a bot finishes running totally.



I'm running a instagram bot from my own residential network. It is configured in a way that after all the tasks are done it simply stops running.



And I would like my computer to shutdown after the bot stops running in order to save energy. For example, when the bot is done something like 'Session Ended' shows up on terminal. I would like the computer to shutdown after this.



I've seen questions related to this, but they were according to a torrent or something, and the computer would shutdown after the download is completed.



In this case it's simply something that is running in the terminal and I want the computer to shutdown when that task is totally done.










share|improve this question































    1















    I would like to know how can I schedule a shutdown in my computer after a bot finishes running totally.



    I'm running a instagram bot from my own residential network. It is configured in a way that after all the tasks are done it simply stops running.



    And I would like my computer to shutdown after the bot stops running in order to save energy. For example, when the bot is done something like 'Session Ended' shows up on terminal. I would like the computer to shutdown after this.



    I've seen questions related to this, but they were according to a torrent or something, and the computer would shutdown after the download is completed.



    In this case it's simply something that is running in the terminal and I want the computer to shutdown when that task is totally done.










    share|improve this question



























      1












      1








      1








      I would like to know how can I schedule a shutdown in my computer after a bot finishes running totally.



      I'm running a instagram bot from my own residential network. It is configured in a way that after all the tasks are done it simply stops running.



      And I would like my computer to shutdown after the bot stops running in order to save energy. For example, when the bot is done something like 'Session Ended' shows up on terminal. I would like the computer to shutdown after this.



      I've seen questions related to this, but they were according to a torrent or something, and the computer would shutdown after the download is completed.



      In this case it's simply something that is running in the terminal and I want the computer to shutdown when that task is totally done.










      share|improve this question
















      I would like to know how can I schedule a shutdown in my computer after a bot finishes running totally.



      I'm running a instagram bot from my own residential network. It is configured in a way that after all the tasks are done it simply stops running.



      And I would like my computer to shutdown after the bot stops running in order to save energy. For example, when the bot is done something like 'Session Ended' shows up on terminal. I would like the computer to shutdown after this.



      I've seen questions related to this, but they were according to a torrent or something, and the computer would shutdown after the download is completed.



      In this case it's simply something that is running in the terminal and I want the computer to shutdown when that task is totally done.







      command-line shutdown






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 29 at 15:23









      pomsky

      33.7k11105137




      33.7k11105137










      asked Mar 29 at 10:27









      Daniel PáscoaDaniel Páscoa

      83




      83






















          2 Answers
          2






          active

          oldest

          votes


















          1














          One way to do this: first, find the PID (process ID) of the running process, using pgrep [name of script, or program].



          Then write another script:



          #!/bin/bash
          while ps -p $PID; do sleep 1; done ; shutdown -h


          Replace $PID by the actual process ID.
          What the script does is: look if the process with the ID still exists. If so, sleep a second and look again. If not, proceed to shut down the system



          Save it (as myscript.sh, say), make it executable (chmod +x myscript.sh) and run it with root permission: sudo myscript.sh. You need root permission to do the shutdown command.






          share|improve this answer
























          • Thanks for your answer! I understand what you say, but when I try this: pgrep [name of script, or program] , the terminal shows a huge list of numbers from 1 to 5115, one in each line... How do I know which one corresponds to the PID that we're trying to find? Thank you once again!

            – Daniel Páscoa
            Mar 29 at 11:02











          • If your bot executable is called mybot , then pgrep mybot should output just one line, or perhaps a few if it uses child processes.

            – Jos
            Mar 29 at 11:52











          • I think I know what the problem is. I run the bot from a file called mybot.py , it is not executable, can I still find the process id? Thank you

            – Daniel Páscoa
            Mar 29 at 12:02











          • Sure: just do ps -ef | grep mybot.py. The first number is the PID.

            – Jos
            Mar 29 at 12:03













          • Thanks! I got 3 lines: 3520 1634 1 11:55 ? 00:00:07 gedit /home/dpascoa/Desktop/instapy-quickstart-master/test.py 3792 3551 0 12:05 pts/0 00:00:00 python3 test.py 3965 3956 0 12:06 pts/1 00:00:00 grep --color=auto test.py Which one is the right one?

            – Daniel Páscoa
            Mar 29 at 12:09



















          2














          Just append shutdown to the command with the && operator. && would make sure the system shuts down only if the command exits succesfully. So you need to run



          your-command && shutdown





          share|improve this answer
























          • Great! I've not wondered that! You mean simply putting all the lines of the code inside (), and then shutdown? Is it also possible to shutdown after 30 mins of the command finishes?

            – Daniel Páscoa
            Mar 29 at 10:40













          • What I meant was run the command your-script && shutdown instead of just your-script. You can also add a delay of 30 minutes by adding sleep 30m && before shutdown.

            – pomsky
            Mar 29 at 15:26








          • 1





            ..or give minutes value as an argument in shutdown command, like shutdown 30 or some specific time (in 24 hrs format), like shutdown 16:00. In first case, shutdown will occur after 30min and in second case at 4:00pm

            – Kulfy
            Mar 29 at 15:29













          • Thanks! It is working fine now :)

            – Daniel Páscoa
            Mar 29 at 23:25











          • @DanielPáscoa If you find this answer useful, please consider "accepting" it (by clicking the tick (✓) next to it) to indicate you've found a working solution and also so that others may more easily find it in the future.

            – pomsky
            Mar 30 at 1:09












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "89"
          };
          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1129645%2fhow-do-i-shutdown-the-computer-automatically-after-bot-finishes-running-from-te%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          One way to do this: first, find the PID (process ID) of the running process, using pgrep [name of script, or program].



          Then write another script:



          #!/bin/bash
          while ps -p $PID; do sleep 1; done ; shutdown -h


          Replace $PID by the actual process ID.
          What the script does is: look if the process with the ID still exists. If so, sleep a second and look again. If not, proceed to shut down the system



          Save it (as myscript.sh, say), make it executable (chmod +x myscript.sh) and run it with root permission: sudo myscript.sh. You need root permission to do the shutdown command.






          share|improve this answer
























          • Thanks for your answer! I understand what you say, but when I try this: pgrep [name of script, or program] , the terminal shows a huge list of numbers from 1 to 5115, one in each line... How do I know which one corresponds to the PID that we're trying to find? Thank you once again!

            – Daniel Páscoa
            Mar 29 at 11:02











          • If your bot executable is called mybot , then pgrep mybot should output just one line, or perhaps a few if it uses child processes.

            – Jos
            Mar 29 at 11:52











          • I think I know what the problem is. I run the bot from a file called mybot.py , it is not executable, can I still find the process id? Thank you

            – Daniel Páscoa
            Mar 29 at 12:02











          • Sure: just do ps -ef | grep mybot.py. The first number is the PID.

            – Jos
            Mar 29 at 12:03













          • Thanks! I got 3 lines: 3520 1634 1 11:55 ? 00:00:07 gedit /home/dpascoa/Desktop/instapy-quickstart-master/test.py 3792 3551 0 12:05 pts/0 00:00:00 python3 test.py 3965 3956 0 12:06 pts/1 00:00:00 grep --color=auto test.py Which one is the right one?

            – Daniel Páscoa
            Mar 29 at 12:09
















          1














          One way to do this: first, find the PID (process ID) of the running process, using pgrep [name of script, or program].



          Then write another script:



          #!/bin/bash
          while ps -p $PID; do sleep 1; done ; shutdown -h


          Replace $PID by the actual process ID.
          What the script does is: look if the process with the ID still exists. If so, sleep a second and look again. If not, proceed to shut down the system



          Save it (as myscript.sh, say), make it executable (chmod +x myscript.sh) and run it with root permission: sudo myscript.sh. You need root permission to do the shutdown command.






          share|improve this answer
























          • Thanks for your answer! I understand what you say, but when I try this: pgrep [name of script, or program] , the terminal shows a huge list of numbers from 1 to 5115, one in each line... How do I know which one corresponds to the PID that we're trying to find? Thank you once again!

            – Daniel Páscoa
            Mar 29 at 11:02











          • If your bot executable is called mybot , then pgrep mybot should output just one line, or perhaps a few if it uses child processes.

            – Jos
            Mar 29 at 11:52











          • I think I know what the problem is. I run the bot from a file called mybot.py , it is not executable, can I still find the process id? Thank you

            – Daniel Páscoa
            Mar 29 at 12:02











          • Sure: just do ps -ef | grep mybot.py. The first number is the PID.

            – Jos
            Mar 29 at 12:03













          • Thanks! I got 3 lines: 3520 1634 1 11:55 ? 00:00:07 gedit /home/dpascoa/Desktop/instapy-quickstart-master/test.py 3792 3551 0 12:05 pts/0 00:00:00 python3 test.py 3965 3956 0 12:06 pts/1 00:00:00 grep --color=auto test.py Which one is the right one?

            – Daniel Páscoa
            Mar 29 at 12:09














          1












          1








          1







          One way to do this: first, find the PID (process ID) of the running process, using pgrep [name of script, or program].



          Then write another script:



          #!/bin/bash
          while ps -p $PID; do sleep 1; done ; shutdown -h


          Replace $PID by the actual process ID.
          What the script does is: look if the process with the ID still exists. If so, sleep a second and look again. If not, proceed to shut down the system



          Save it (as myscript.sh, say), make it executable (chmod +x myscript.sh) and run it with root permission: sudo myscript.sh. You need root permission to do the shutdown command.






          share|improve this answer













          One way to do this: first, find the PID (process ID) of the running process, using pgrep [name of script, or program].



          Then write another script:



          #!/bin/bash
          while ps -p $PID; do sleep 1; done ; shutdown -h


          Replace $PID by the actual process ID.
          What the script does is: look if the process with the ID still exists. If so, sleep a second and look again. If not, proceed to shut down the system



          Save it (as myscript.sh, say), make it executable (chmod +x myscript.sh) and run it with root permission: sudo myscript.sh. You need root permission to do the shutdown command.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 29 at 10:46









          JosJos

          14.9k54152




          14.9k54152













          • Thanks for your answer! I understand what you say, but when I try this: pgrep [name of script, or program] , the terminal shows a huge list of numbers from 1 to 5115, one in each line... How do I know which one corresponds to the PID that we're trying to find? Thank you once again!

            – Daniel Páscoa
            Mar 29 at 11:02











          • If your bot executable is called mybot , then pgrep mybot should output just one line, or perhaps a few if it uses child processes.

            – Jos
            Mar 29 at 11:52











          • I think I know what the problem is. I run the bot from a file called mybot.py , it is not executable, can I still find the process id? Thank you

            – Daniel Páscoa
            Mar 29 at 12:02











          • Sure: just do ps -ef | grep mybot.py. The first number is the PID.

            – Jos
            Mar 29 at 12:03













          • Thanks! I got 3 lines: 3520 1634 1 11:55 ? 00:00:07 gedit /home/dpascoa/Desktop/instapy-quickstart-master/test.py 3792 3551 0 12:05 pts/0 00:00:00 python3 test.py 3965 3956 0 12:06 pts/1 00:00:00 grep --color=auto test.py Which one is the right one?

            – Daniel Páscoa
            Mar 29 at 12:09



















          • Thanks for your answer! I understand what you say, but when I try this: pgrep [name of script, or program] , the terminal shows a huge list of numbers from 1 to 5115, one in each line... How do I know which one corresponds to the PID that we're trying to find? Thank you once again!

            – Daniel Páscoa
            Mar 29 at 11:02











          • If your bot executable is called mybot , then pgrep mybot should output just one line, or perhaps a few if it uses child processes.

            – Jos
            Mar 29 at 11:52











          • I think I know what the problem is. I run the bot from a file called mybot.py , it is not executable, can I still find the process id? Thank you

            – Daniel Páscoa
            Mar 29 at 12:02











          • Sure: just do ps -ef | grep mybot.py. The first number is the PID.

            – Jos
            Mar 29 at 12:03













          • Thanks! I got 3 lines: 3520 1634 1 11:55 ? 00:00:07 gedit /home/dpascoa/Desktop/instapy-quickstart-master/test.py 3792 3551 0 12:05 pts/0 00:00:00 python3 test.py 3965 3956 0 12:06 pts/1 00:00:00 grep --color=auto test.py Which one is the right one?

            – Daniel Páscoa
            Mar 29 at 12:09

















          Thanks for your answer! I understand what you say, but when I try this: pgrep [name of script, or program] , the terminal shows a huge list of numbers from 1 to 5115, one in each line... How do I know which one corresponds to the PID that we're trying to find? Thank you once again!

          – Daniel Páscoa
          Mar 29 at 11:02





          Thanks for your answer! I understand what you say, but when I try this: pgrep [name of script, or program] , the terminal shows a huge list of numbers from 1 to 5115, one in each line... How do I know which one corresponds to the PID that we're trying to find? Thank you once again!

          – Daniel Páscoa
          Mar 29 at 11:02













          If your bot executable is called mybot , then pgrep mybot should output just one line, or perhaps a few if it uses child processes.

          – Jos
          Mar 29 at 11:52





          If your bot executable is called mybot , then pgrep mybot should output just one line, or perhaps a few if it uses child processes.

          – Jos
          Mar 29 at 11:52













          I think I know what the problem is. I run the bot from a file called mybot.py , it is not executable, can I still find the process id? Thank you

          – Daniel Páscoa
          Mar 29 at 12:02





          I think I know what the problem is. I run the bot from a file called mybot.py , it is not executable, can I still find the process id? Thank you

          – Daniel Páscoa
          Mar 29 at 12:02













          Sure: just do ps -ef | grep mybot.py. The first number is the PID.

          – Jos
          Mar 29 at 12:03







          Sure: just do ps -ef | grep mybot.py. The first number is the PID.

          – Jos
          Mar 29 at 12:03















          Thanks! I got 3 lines: 3520 1634 1 11:55 ? 00:00:07 gedit /home/dpascoa/Desktop/instapy-quickstart-master/test.py 3792 3551 0 12:05 pts/0 00:00:00 python3 test.py 3965 3956 0 12:06 pts/1 00:00:00 grep --color=auto test.py Which one is the right one?

          – Daniel Páscoa
          Mar 29 at 12:09





          Thanks! I got 3 lines: 3520 1634 1 11:55 ? 00:00:07 gedit /home/dpascoa/Desktop/instapy-quickstart-master/test.py 3792 3551 0 12:05 pts/0 00:00:00 python3 test.py 3965 3956 0 12:06 pts/1 00:00:00 grep --color=auto test.py Which one is the right one?

          – Daniel Páscoa
          Mar 29 at 12:09













          2














          Just append shutdown to the command with the && operator. && would make sure the system shuts down only if the command exits succesfully. So you need to run



          your-command && shutdown





          share|improve this answer
























          • Great! I've not wondered that! You mean simply putting all the lines of the code inside (), and then shutdown? Is it also possible to shutdown after 30 mins of the command finishes?

            – Daniel Páscoa
            Mar 29 at 10:40













          • What I meant was run the command your-script && shutdown instead of just your-script. You can also add a delay of 30 minutes by adding sleep 30m && before shutdown.

            – pomsky
            Mar 29 at 15:26








          • 1





            ..or give minutes value as an argument in shutdown command, like shutdown 30 or some specific time (in 24 hrs format), like shutdown 16:00. In first case, shutdown will occur after 30min and in second case at 4:00pm

            – Kulfy
            Mar 29 at 15:29













          • Thanks! It is working fine now :)

            – Daniel Páscoa
            Mar 29 at 23:25











          • @DanielPáscoa If you find this answer useful, please consider "accepting" it (by clicking the tick (✓) next to it) to indicate you've found a working solution and also so that others may more easily find it in the future.

            – pomsky
            Mar 30 at 1:09
















          2














          Just append shutdown to the command with the && operator. && would make sure the system shuts down only if the command exits succesfully. So you need to run



          your-command && shutdown





          share|improve this answer
























          • Great! I've not wondered that! You mean simply putting all the lines of the code inside (), and then shutdown? Is it also possible to shutdown after 30 mins of the command finishes?

            – Daniel Páscoa
            Mar 29 at 10:40













          • What I meant was run the command your-script && shutdown instead of just your-script. You can also add a delay of 30 minutes by adding sleep 30m && before shutdown.

            – pomsky
            Mar 29 at 15:26








          • 1





            ..or give minutes value as an argument in shutdown command, like shutdown 30 or some specific time (in 24 hrs format), like shutdown 16:00. In first case, shutdown will occur after 30min and in second case at 4:00pm

            – Kulfy
            Mar 29 at 15:29













          • Thanks! It is working fine now :)

            – Daniel Páscoa
            Mar 29 at 23:25











          • @DanielPáscoa If you find this answer useful, please consider "accepting" it (by clicking the tick (✓) next to it) to indicate you've found a working solution and also so that others may more easily find it in the future.

            – pomsky
            Mar 30 at 1:09














          2












          2








          2







          Just append shutdown to the command with the && operator. && would make sure the system shuts down only if the command exits succesfully. So you need to run



          your-command && shutdown





          share|improve this answer













          Just append shutdown to the command with the && operator. && would make sure the system shuts down only if the command exits succesfully. So you need to run



          your-command && shutdown






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 29 at 10:35









          pomskypomsky

          33.7k11105137




          33.7k11105137













          • Great! I've not wondered that! You mean simply putting all the lines of the code inside (), and then shutdown? Is it also possible to shutdown after 30 mins of the command finishes?

            – Daniel Páscoa
            Mar 29 at 10:40













          • What I meant was run the command your-script && shutdown instead of just your-script. You can also add a delay of 30 minutes by adding sleep 30m && before shutdown.

            – pomsky
            Mar 29 at 15:26








          • 1





            ..or give minutes value as an argument in shutdown command, like shutdown 30 or some specific time (in 24 hrs format), like shutdown 16:00. In first case, shutdown will occur after 30min and in second case at 4:00pm

            – Kulfy
            Mar 29 at 15:29













          • Thanks! It is working fine now :)

            – Daniel Páscoa
            Mar 29 at 23:25











          • @DanielPáscoa If you find this answer useful, please consider "accepting" it (by clicking the tick (✓) next to it) to indicate you've found a working solution and also so that others may more easily find it in the future.

            – pomsky
            Mar 30 at 1:09



















          • Great! I've not wondered that! You mean simply putting all the lines of the code inside (), and then shutdown? Is it also possible to shutdown after 30 mins of the command finishes?

            – Daniel Páscoa
            Mar 29 at 10:40













          • What I meant was run the command your-script && shutdown instead of just your-script. You can also add a delay of 30 minutes by adding sleep 30m && before shutdown.

            – pomsky
            Mar 29 at 15:26








          • 1





            ..or give minutes value as an argument in shutdown command, like shutdown 30 or some specific time (in 24 hrs format), like shutdown 16:00. In first case, shutdown will occur after 30min and in second case at 4:00pm

            – Kulfy
            Mar 29 at 15:29













          • Thanks! It is working fine now :)

            – Daniel Páscoa
            Mar 29 at 23:25











          • @DanielPáscoa If you find this answer useful, please consider "accepting" it (by clicking the tick (✓) next to it) to indicate you've found a working solution and also so that others may more easily find it in the future.

            – pomsky
            Mar 30 at 1:09

















          Great! I've not wondered that! You mean simply putting all the lines of the code inside (), and then shutdown? Is it also possible to shutdown after 30 mins of the command finishes?

          – Daniel Páscoa
          Mar 29 at 10:40







          Great! I've not wondered that! You mean simply putting all the lines of the code inside (), and then shutdown? Is it also possible to shutdown after 30 mins of the command finishes?

          – Daniel Páscoa
          Mar 29 at 10:40















          What I meant was run the command your-script && shutdown instead of just your-script. You can also add a delay of 30 minutes by adding sleep 30m && before shutdown.

          – pomsky
          Mar 29 at 15:26







          What I meant was run the command your-script && shutdown instead of just your-script. You can also add a delay of 30 minutes by adding sleep 30m && before shutdown.

          – pomsky
          Mar 29 at 15:26






          1




          1





          ..or give minutes value as an argument in shutdown command, like shutdown 30 or some specific time (in 24 hrs format), like shutdown 16:00. In first case, shutdown will occur after 30min and in second case at 4:00pm

          – Kulfy
          Mar 29 at 15:29







          ..or give minutes value as an argument in shutdown command, like shutdown 30 or some specific time (in 24 hrs format), like shutdown 16:00. In first case, shutdown will occur after 30min and in second case at 4:00pm

          – Kulfy
          Mar 29 at 15:29















          Thanks! It is working fine now :)

          – Daniel Páscoa
          Mar 29 at 23:25





          Thanks! It is working fine now :)

          – Daniel Páscoa
          Mar 29 at 23:25













          @DanielPáscoa If you find this answer useful, please consider "accepting" it (by clicking the tick (✓) next to it) to indicate you've found a working solution and also so that others may more easily find it in the future.

          – pomsky
          Mar 30 at 1:09





          @DanielPáscoa If you find this answer useful, please consider "accepting" it (by clicking the tick (✓) next to it) to indicate you've found a working solution and also so that others may more easily find it in the future.

          – pomsky
          Mar 30 at 1:09


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Ask Ubuntu!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1129645%2fhow-do-i-shutdown-the-computer-automatically-after-bot-finishes-running-from-te%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Category:香港粉麵

          List *all* the tuples!

          Channel [V]