Skip to content Skip to sidebar Skip to footer

How To Send Control C To Mac Terminal Using Python?

I have a python script that needs to send control C to the mac terminal. I've tried sending the plain text '^C' but I get back that the terminal does not recognize the command. (Th

Solution 1:

You can explicitly send the SIGINT signal to the process if you can get its PID using os.kill.

os.kill(pid, signal.SIGINT)

This will require you to instrument your script to grab the process PID, but it's the best way to emulate the "ctrl-c" behavior.

Solution 2:

If you open the process using subprocess's Popen, you should be able to send a control signal like this:

proc.send_signal(signal.SIGINT)

You'll need to import signal to get SIGINT.

Post a Comment for "How To Send Control C To Mac Terminal Using Python?"