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.
Related
I am new here but I recently have been messing around with python and Qt. My situation is that one of the scripts I call does a lot of OS commands and basically waits for a response. When I call this script it runs fine and acts accordingly except in my main program the screen is frozen until I exit out of the cmd. I think this is because mt script just waits there for a response, is there anyway to make it so that even though the script is running and executing(waiting for response with cmd) the user can still use other aspects of the main program?
As mentioned in comments, you will need to use threading. Threading allows multiple functions to executed at the same time. Check out this link Python threading.
You'll just have to run your side script on a different thread.
I have a python script which launches a sequence of subprocesses in a loop:
for c in cmds:
subprocess.call(c, shell=True)
When the script is running in terminal, I try to stop it with Ctrl+C or Ctrl+D, however, it will continue to launch next subprocess. How can I terminate the whole python script at once?
What likely happens is that ctrl-C is intercepted by you sub-process (running on the same terminal) and never gets to your Python script. What you can do is to check the return value of subprocess.call to see whether the child process was killed by a signal and decide whether you want to stop submitting new processes.
I'm trying to write a Python code that is to check on network settings on a network switch. Expecting errors, I have included 'exit.sys()' commands in case an error or non-matching data shows up. However, while I want it to terminate and throw an exception in the script, it also terminates the CLI session (Putty). What would be the best command to be able to throw an exception and terminate only the script that is running on the switch without terminating Putty?
if isc_port.endswith('g'):
isc_port = isc_port[:-1]
elif isc_port.endswith('G'):
sys.exit("The ISC port is also configured as an MLAG port. Please correct.")
#return ELM
Calling sys.exit() only exits the (python) process. It does not (directly) affect putty or any other terminal (xterm, gnome-terminal, etc.).
PuTTY terminates when the connection it has is closed by the remote service. Often, eg with ssh or telnet, this occurs when the program that is run by logging in terminates. Usually the program is a shell (eg /bin/sh, /bin/bash, etc.), which when run interactively does not terminate until you type 'exit' (or send it EOF, typically Ctrl-D). When a shell is run non-interatively it will usually terminate when the command it runs terminates.
Perhaps you are using your python program as the shell when you log in? In that case, don't exit when you don't want to exit. Perhaps you have a shell script that runs your program, then exits, when you log in? If so, change that script to not exit.
Also, I have usually seen PuTTY keep the window open, so all output is still visibile, until you acknowledge the notice that the network connection was lost. It is the Windows DOS/cmd.exe window that I have seen close immediately upon termination of the program that it runs.
I'm working on a project that spins off several long-running workers as processes. Child workers catch SIGINT and clean up after themselves - based on my research, this is considered a best practice, and works as expected when terminating scripts.
I am actively developing this project, which means that I am regularly testing changes in the interpreter. When I'm working in an interpreter, I often hit CTRL+C to clear currently written text and get a fresh prompt. Unfortunately, if I do this while a subprocess is running, SIGINT is sent to that worker, causing it to terminate.
Is there a solution to this problem other than "never hit CTRL+C in your interpreter"?
One option is to set a variable (e.g. environment variable, commandline option) when debugging.
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.