0

I am trying to write a downloader script (placed in unity luncher) using python that calls wget with all the right arguments. The script extracts url from clipboard and the file name from gtk primary clipboard, the one operated by text selection or copy and middle mouse click for paste. The code is rather simple.

import gtk
from os import system as sys

url = str(gtk.clipboard_get().wait_for_text())
name = str(gtk.clipboard_get(gtk.gdk.SELECTION_PRIMARY).wait_for_text())

if name.lower()=='none' :
    sys("/usr/bin/canberra-gtk-play --id='dialog-warning'")
    exit(1)

sys("/usr/bin/canberra-gtk-play --id='downloading'")
com='wget -c -t 0 "%s" -O "%s"' % (url,name)
sys("gnome-terminal -e '%s'" % com)

the script opens a terminal window and pints the wget output. The problem is that closing the gnome-terminal doesnt cause wget to exit, rather it runs in the background. Is it possible to stop this from happening

CuriousCat
  • 429
  • 5
  • 16

1 Answers1

0

The problem is that, by design, wget ignores the SIGHUP which is sent when its parent process terminates.

One solution would be to use the python signal module to catch the SIGCHLD which should be sent to your script when you close the terminal window, and register a handler to explicitly send a SIGINT or SIGTERM to wget.

Aya
  • 39,884
  • 6
  • 55
  • 55
  • How can i do it. can you show with an example, i will incorporate it in the code – CuriousCat May 04 '13 at 06:01
  • @SaketDandawate If you're not familiar with signals, it might be simpler to run `wget` with a script like [this](https://gist.github.com/unakatsuo/1048855) with `hup2term wget `, which converts the parent process's `SIGHUP` to a `SIGTERM`, or just close the window with CTRL-C (to send a `SIGINT` to `wget`) instead of closing the terminal window. – Aya May 04 '13 at 15:42