Sending Cntrl-C ?? - Python

This is a discussion on Sending Cntrl-C ?? - Python ; Hi, I really like this recipe for controlling subprocesses: http://aspn.activestate.com/ASPN/Coo.../Recipe/440554 However, I can't figure out how I can send the equivalent of "Cntrl-C" to the subprocess. How can that be done? TIA, -T...

+ Reply to Thread
Results 1 to 8 of 8

Sending Cntrl-C ??

  1. Default Sending Cntrl-C ??

    Hi,

    I really like this recipe for controlling subprocesses:
    http://aspn.activestate.com/ASPN/Coo.../Recipe/440554

    However, I can't figure out how I can send the equivalent of "Cntrl-C"
    to the subprocess. How can that be done?

    TIA,
    -T

  2. Default Re: Sending Cntrl-C ??

    gamename schrieb:
    > Hi,
    >
    > I really like this recipe for controlling subprocesses:
    > http://aspn.activestate.com/ASPN/Coo.../Recipe/440554
    >
    > However, I can't figure out how I can send the equivalent of "Cntrl-C"
    > to the subprocess. How can that be done?


    import os
    import signal
    import subprocess

    popen = subprocess(...)
    os.kill(popen.pid, signal.SIGINT)

    Or with Python 2.6+:

    popen.send_signal(signal.SIGINT)

    Christian


  3. Default Re: Sending Cntrl-C ??


    > import os
    > import signal
    > import subprocess
    >
    > popen = subprocess(...)
    > os.kill(popen.pid, signal.SIGINT)
    >
    > Or with Python 2.6+:
    >
    > popen.send_signal(signal.SIGINT)


    Thanks, Christian. Would that work on win32 as well?

    -T

  4. Default Re: Sending Cntrl-C ??

    gamename schrieb:
    > Thanks, Christian. Would that work on win32 as well?


    No, Windows doesn't support the same, rich set of signal as Unix OSes.

    Christian


  5. Default Re: Sending Cntrl-C ??

    En Tue, 29 Apr 2008 20:48:42 -0300, Christian Heimes <lists@cheimes.de>
    escribió:

    > gamename schrieb:
    >> Thanks, Christian. Would that work on win32 as well?

    >
    > No, Windows doesn't support the same, rich set of signal as Unix OSes.


    True but irrelevant to the question.
    To the OP: you can download the pywin32 package from sourceforge, and use
    win32api.GenerateConsoleCtrlEvent(win32con.CTRL_C_EVENT, pgid)
    or call the same function using ctypes.
    See http://msdn.microsoft.com/en-us/libr...55(VS.85).aspx for some
    important remarks.

    --
    Gabriel Genellina


  6. Default Re: Sending Cntrl-C ??

    >
    > > No, Windows doesn't support the same, rich set of signal as Unix OSes.

    >
    > True but irrelevant to the question.
    > To the OP: you can download the pywin32 package from sourceforge, and use
    > win32api.GenerateConsoleCtrlEvent(win32con.CTRL_C_EVENT, pgid)
    > or call the same function using ctypes.
    > Seehttp://msdn.microsoft.com/en-us/library/ms683155(VS.85).aspxfor some
    > important remarks.


    Thanks, guys. Good info.
    -T

  7. Default Re: Sending Cntrl-C ??

    > win32api.GenerateConsoleCtrlEvent(win32con.CTRL_C_EVENT, pgid)

    How do you determine the value of 'pgid'?


  8. Default Re: Sending Cntrl-C ??

    En Wed, 30 Apr 2008 15:06:13 -0300, gamename <namesagame-usenet@yahoo.com>
    escribió:

    >> win32api.GenerateConsoleCtrlEvent(win32con.CTRL_C_EVENT, pgid)

    >
    > How do you determine the value of 'pgid'?


    Make the child start a new process group, then its pid is the process
    group ID. You have to use the "creationflags" parameter of subprocess.open
    The documentation for GenerateConsoleCtrlEvent
    http://msdn.microsoft.com/en-us/library/ms683155.aspx states that you
    can't send CTRL_C_EVENT to another process group, only CTRL_BREAK_EVENT
    (and only to the same console as the sender process). A little example:

    <code>
    import subprocess
    import ctypes
    import time

    CREATE_NEW_PROCESS_GROUP = 512
    CTRL_C_EVENT = 0
    CTRL_BREAK_EVENT = 1
    GenerateConsoleCtrlEvent = ctypes.windll.kernel32.GenerateConsoleCtrlEvent

    print "start child process"
    p = subprocess.Popen("cmd /c for /L %d in (10,-1,0) do @(echo %d && sleep
    1)",
    creationflags = CREATE_NEW_PROCESS_GROUP)
    print "pid=", p.pid
    print "wait 3 secs"
    time.sleep(3)
    print "send Ctrl-Break"
    GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, p.pid)
    print "wait for child to stop"
    print "retcode=", p.wait()
    print "done"
    </code>

    Output:

    start child process
    pid= 872
    wait 3 secs
    10
    9
    8
    7
    send Ctrl-Break
    wait for child to stop
    retcode= 255
    done

    (Instead of ctypes and those magical constants, you can install the
    pywin32 package and use win32api.GenerateConsoleCtrlEvent,
    win32con.CTRL_BREAK_EVENT and win32process.CREATE_NEW_PROCESS_GROUP)

    The only way I know of to send a Ctrl-C event to a different console
    involves remote code injection.

    --
    Gabriel Genellina


+ Reply to Thread