Results 1 to 3 of 3

Thread: Python close Terminal window

  1. #1
    Join Date
    Sep 2013
    Beans
    9

    Python close Terminal window

    Hey Everybody,

    I need a way for a CLI python script to be able to close a terminal windows after it finishes. I think the easiest way to do this would be to simulate keypresses and use Control + Shift + Q , but I've already tried two ways of doing this, and neither worked. sys.exit leaves the terminal window open.


    Thanks!
    Evamvid
    Last edited by evamvid; September 24th, 2013 at 09:44 PM. Reason: Accidentally posted two early

  2. #2
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: Python close Terminal window

    Could you exec the python interpreter so that it replaces the current shell process?

    For example:

    test.py
    Code:
    import time
    time.sleep(3)
    Code:
    $ exec python test.py
    I'd probably be annoyed if I ran a script that closed the terminal window but it depends on the context I guess.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  3. #3
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,160
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Python close Terminal window

    Kill your parent process: read /proc/self/status(*), get the PPid value... warning: there are environments with terminals in tabs (Konsole in KDE) and you could be killing other sessions...).

    (*) "self" is the process that opens the file, so you cannot use a subprocess to read, otherwise PPid will be your own. But if you know your own PID, you can read /proc/$pid/status instead form any process.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •