0

I'm writing a program that at 3AM, it reads everything from a file and downloads the files from the links one by one, and stops doing so (pauses the download) if it's 7AM

But my problem is If I use os.system("wget name") I won't be able to stop it, I can't put a flag in a while loop to check it because os.system() won't finish until the download is complete. I can't think of any other way. My net is unstable so I assume I have to use wget, I heard of urllib but I'm not sure if it will work with my unstable connection!

I'm planning on running this on ArchLinux on my raspberry Pi

Kevin
  • 74,910
  • 12
  • 133
  • 166
Amy Gamble
  • 175
  • 1
  • 10
  • 1
    use `handle = subprocess.Popen(['wget', 'name'])` instead. The `handle` has a function called `.poll()` to check status and `.terminate()` to terminate the process. There's also a `send_signal(SIGNAL)` but all of this is dangerous if not done correctly. – Torxed Mar 11 '16 at 17:04
  • 1
    And then the killed subprocess still runs in the background? Guess he needs something like a process group described here: http://stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launched-with-shell-true – Rafael T Mar 11 '16 at 17:04
  • Thanks a lot! that will work I guess, It's too late now I will write the code tomarrow! – Amy Gamble Mar 11 '16 at 17:34

1 Answers1

1

You have a subprocess you want to stop at 7 AM. Popen.wait has a timeout so all you have to do is figure out the timeout and use it.

import subprocess
import datetime
import time

now = datetime.datetime.now()
stopat = now.replace(hour=7, minute=0, second=0, microsecond=0)
delta = stopat.timestamp() - now.timestamp()
if delta > 0:
    proc = subprocess.Popen("wget name", shell=True)
    try:
        proc.wait(delta)
    except subprocess.TimeoutExpired:
        proc.terminate()
        try:
            proc.wait(2)
        except subprocess.TimeoutExpired:
            proc.kill()
tdelaney
  • 73,364
  • 6
  • 83
  • 116