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.

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.

| 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 '19 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 '19 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 '19 at 12:02
  • Sure: just do ps -ef | grep mybot.py. The first number is the PID. – Jos Mar 29 '19 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 '19 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
| 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 '19 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 '19 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 '19 at 15:29
  • Thanks! It is working fine now :) – Daniel Páscoa Mar 29 '19 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 '19 at 1:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.