killing a process in windows

Discussion in 'Python' started by Stephen Boulet, Oct 17, 2003.

  1. This is wierd. I can launch and kill a process in windows, but only if I do
    it in the same script.

    Just using xemacs as an example, I can launch it like so:

    import os, win32api, sys,os.path
    prog = "xemacs.exe"
    command = os.path.join(base,prog)
    mode = os.P_NOWAIT
    id = os.spawnl(mode,command)

    and then kill it:
    win32api.TerminateProcess(id,0)

    This works. But I want the script to launch the program if the script is
    passed the argument "start", or kill it otherwise. Since there's no way
    that I know to query the process ID if python doesn't launch the program in
    the first place, I write the ID to a temp file and then read the id to kill
    the process. But this doesn't work:

    import os, win32api, sys,os.path

    argument = sys.argv[1]

    base = "C:\\Program Files\\XEmacs\\XEmacs-21.4.13\\i586-pc-win32"
    if argument == 'start':
    prog = "xemacs.exe"
    command = os.path.join(base,prog)

    mode = os.P_NOWAIT
    id = os.spawnl(mode,command)
    f = open(os.path.join(base,'temp.txt'),'wb')
    f.write(str(id))
    f.close()
    else:
    id = int(open(os.path.join(base,'temp.txt')).read())
    win32api.TerminateProcess(id,0)

    Sorry I don't remember the error exactly, but it involved 'improper handle'.

    Is there something I'm missing? Is there a way to query for running process
    IDs? Thanks.

    --
    Stephen
    From here to there
    and there to here,
    funny things are everywhere. -- Dr Seuss
     
    Stephen Boulet, Oct 17, 2003
    #1
    1. Advertising

  2. The exact error is:

    >>> win32api.TerminateProcess(100,0)

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    pywintypes.error: (6, 'TerminateProcess', 'The handle is invalid.')

    What seems to be happening is that exiting a python script (leaving a
    process running, let's call it "process X", that I launched with the
    script) and starting a new script changes the value of the process ID
    for "process X".

    Stephen Boulet wrote:
    > This is wierd. I can launch and kill a process in windows, but only if I do
    > it in the same script.
    >
    > Just using xemacs as an example, I can launch it like so:
    >
    > import os, win32api, sys,os.path
    > prog = "xemacs.exe"
    > command = os.path.join(base,prog)
    > mode = os.P_NOWAIT
    > id = os.spawnl(mode,command)
    >
    > and then kill it:
    > win32api.TerminateProcess(id,0)
    >
    > This works. But I want the script to launch the program if the script is
    > passed the argument "start", or kill it otherwise. Since there's no way
    > that I know to query the process ID if python doesn't launch the program in
    > the first place, I write the ID to a temp file and then read the id to kill
    > the process. But this doesn't work:
    >
    > import os, win32api, sys,os.path
    >
    > argument = sys.argv[1]
    >
    > base = "C:\\Program Files\\XEmacs\\XEmacs-21.4.13\\i586-pc-win32"
    > if argument == 'start':
    > prog = "xemacs.exe"
    > command = os.path.join(base,prog)
    >
    > mode = os.P_NOWAIT
    > id = os.spawnl(mode,command)
    > f = open(os.path.join(base,'temp.txt'),'wb')
    > f.write(str(id))
    > f.close()
    > else:
    > id = int(open(os.path.join(base,'temp.txt')).read())
    > win32api.TerminateProcess(id,0)
    >
    > Sorry I don't remember the error exactly, but it involved 'improper handle'.
    >
    > Is there something I'm missing? Is there a way to query for running process
    > IDs? Thanks.
    >
     
    Stephen Boulet, Oct 17, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sting

    killing a process in ms windows - newbie

    Sting, Dec 28, 2003, in forum: C Programming
    Replies:
    2
    Views:
    344
    Richard Bos
    Dec 29, 2003
  2. Jani Yusef
    Replies:
    1
    Views:
    318
    omission9
    Jul 1, 2004
  3. Thomas Nelson

    Trouble killing a process on windows

    Thomas Nelson, Jun 2, 2007, in forum: Python
    Replies:
    4
    Views:
    339
    Tim Golden
    Jun 3, 2007
  4. News123

    killing own process in windows

    News123, Mar 7, 2010, in forum: Python
    Replies:
    4
    Views:
    778
    News123
    Mar 7, 2010
  5. Joe Van Dyk

    Killing a windows process

    Joe Van Dyk, Oct 28, 2006, in forum: Ruby
    Replies:
    2
    Views:
    152
Loading...

Share This Page