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. (The terminal meaning the pseudo terminal that python creates)
Basically, I am using the terminal to run an old Unix Executable and the only way that I can think of to terminate this gracefully is to send the interrupt signal. Is there any way I can fool the terminal into thinking that I pressed control C?
Thanks in advance!
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.
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.
Related
I'm trying to automate the restarting of several servers running in iTerm2 tabs using iTerm2's Python API. I can get a reference to the sessions, and issue commands to them (with async_send_text), but I can't figure out how to send SIGINT signals to them so I can issue the restart commands.
I've seen a few answers using AppleScript, but it seems like this should be possible in python.
You can use os.kill() to send signals to a process as long as you have the process ID:
import os
import sys
os.kill(<PID>, signal.SIGINT)
This should work even when the process is in running in a terminal emulator.
I run ffmpeg from python script and need to shutdown recording on demand.
In Linux I just send SIGTERM. But in Windows as I understand SIGTERM replaced by SIGKILL so records needs to be remixed to play properly.
After googling I found that I should use CTRL_BREAK_EVENT but this signal terminate my parent script too.
What should I use?
I need to run Python scripts from SSH on my Raspberry Pi, while still being able to get input from a user on the Raspberry Pi via a USB keyboard emulator (card reader). I would normally be able to use raw_input for this, but if I run the Python script through SSH, it does not create a window and it will not be active so it will receive no input.
Is there any way to ensure a Python script will be active (the top window), even while using SSH to launch it? Or, is there another way to get user input without using raw_input, and works in the background (without an active window)?
Thanks in advance :)
PS: If I have to use other languages (like C) then invoke it in Python, this is fine, I will be able to do that.
I've solved it the best I can, with help from #Gaurav Dave
I now have a script which creates a new terminal window upon launch, using Popen from sys. The script looks like this...
from sys import executable
from subprocess import Popen, CREATE_NEW_CONSOLE
Popen([executable, 'window.py'], creationflags=CREATE_NEW_CONSOLE)
and window.py is simply a test script that prints some text and waits for a certain amount of time...
import time
print("Hello M8!")
time.sleep(5)
window.py will be the script that takes the input as that is the one that has the window.
I am running a script that launches
run_app.py >& log.out
In run_app.py, it will start a few subprocesses and will read stdout/stderr of the subprocesses through pipe. I can run the script fine but if I try to put it into background by:
run_app.py >& log.out &
The run_app.py will hang on reading data from subprocess. It seems that it is similar to this thread:
ffmpeg hangs when run in background
My subprocess also write a lot which might overflow the PIPE_BUF.
However, I am redirecting&writing my stdout/stderr to a file. Are there any suggestions might prevent hanging when I put the script to background while able to save output in a file instead of redirecting them to /dev/null?
When a background process is running, its standard I/O streams are still connected to the screen and keyboard. Processes will be suspended (stopped) if they try to read from the keyboard.
You should have to a message saying something like: Stopped (tty input). That would have been sent to the shell's stderr.
Normally redirecting stdin covers that problem, but some programs access the keyboard directly rather than using stdin, typically those prompting for a password.
I am reading Linux System Programming.
When introducing the system(command) function, the book states that during execution of the command, SIGINT is ignored.
So, assuming that os.system is just a wrapper of the underlying system function, I try the following:
loop.py
while True:
print 'You should not be able to CTRL+C me ;p'
test_loop.py
import os
os.system("python loop.py")
Now that I'm executing loop.py with system, I'm expecting SIGINT to be ignored, but when I use CTRL+C on the running program it still get killed.
Any idea why os.system differ from the system() function?
SIGINT is ignored by the application that calls system (for as long as system is executing). It's not ignored by the application that's spawned by system. So if you hit CTRL+c, that will abort the execution of loop.py, but not of test_loop.py. So if you add some code after the call to system, you'll see that that code will execute after you press CTRL+c.