"ctrl + c" is not recognising while bootup in RHEL 6 - python

Seems Ctrl + c is not recognising .While I run in terminal Ctrl + c is recognised and its going to signal handler function as expected .
but when script is run as boot script it should recognise Ctrl + c and enter
the signal handler ,but its not happening .here is part of script ..
def signal_handler(signal, frame):
print 'Going to Linux terminal....'
sys.exit(0)
print 'Press Ctrl+C to abort script and enter Linux terminal....'
signal.signal(signal.SIGINT, signal_handler)
time.sleep(5)
Script is working proper ,but the problem is its not getting the terminal.
ssh to the termianl is possible ,
Thanks in advance

When you receive the CTRL-C event in you script, your script will just terminate itself. It will not terminate the whole boot sequence.
Checking a boot-script that does exit to a shell (the script that checks the disks with the fsck command) you will see something like this:
# Start a single user shell on the console
if ! sulogin $CONSOLE
then
log_failure_msg "Attempt to start maintenance shell failed.
Continuing with system boot in 5 seconds."
sleep 5
fi
It's the sulogin that does it in this case.

Related

Prevent Python Interpreter from Exiting if CTRL-D is pressed

I am running a script with python -i main.py. The script starts some C threads and python threads using threading module, then python code ends and it goes to a prompt. How can i prevent python from exiting if CTRL-D is accidentally pressed? I don't want to press CTRL-D by accident (already happened twice) and suddenly the interactive interpreter is down with all its threads.
I need the to still have access to the interactive interpreter. The goal is to start the C threads and python threads, then monitor them later from python.
I tried using readline and binding ^D to nothing, but it would still terminate python.
Example (main.py):
print("Init code for python thread")
print("Init code for c thread")
When running this with python -i main.py, after the second line is finished, i get the prompt >>>. If i am at the prompt and press CTRL-D, it will start a systemExit. I want to prevent that at all costs.
If you are waiting on input then you can wrap the input call in try/except. Remember that Ctrl-D is effectively EOF.
Therefore this might be a useful pattern:
while True:
try:
v = input('Type something: ')
break
except EOFError:
print('You typed CTRL-D')
print(f'You typed {v}')

How to stop/terminate a python script from running? (once again)

Context:
I have a running python script. It contains many os.system("./executableNane") calls in a loop.
If I press ctrl + C, it just stops the execution of the current ./executableNane and passes to the next one.
Question:
How to stop the execution of the whole script and not only the execution of the current executable called?
Please note that I have read carefully the question/answer here but even with kill I can kill the executable executableNane but not the whole script (that I cannot find using top).
The only way I have to stop the script (without reboot the system) is to continue to press ctrl + C in a loop as well until all the tests are completed.
You can use subprocess and signal handlers to do this. You can also use subprocess to receive and send information via subprocess.PIPE, which you can read more about in the documentation.
The following should be a basic example of what you are looking to do:
import subprocess
import signal
import sys
def signal_handler(sig, frame):
print("You pressed Ctrl+C, stopping.")
print("Signal: {}".format(sig))
print("Frame: {}".format(frame))
sys.exit(123)
# Set up signal handler
signal.signal(signal.SIGINT, signal_handler)
print("Starting.")
while True:
cmd = ['sleep', '10']
p = subprocess.Popen(cmd)
p.wait()
if p.returncode != 0:
print("Command failed.")
else:
print("Command worked.")
The other solution to the question (by #Quillan Kaseman) is much more elegant compared with the solution I have found. All my problems are solved when I press Ctrl + Z instead of Ctrl + C.
Indeed, I have no idea why with Z works and with C does not. (I will try to look for some details later on).

CMD Python Program not interrupted by Ctrl + C

In my current windows 10 version (10.0.18362) it does not work to cancel a python program with Ctrl + C when running in CMD.
I dont know if it has to do with it, I just want to state that the main Thread has finished but another thread is running when I try to hit Ctrl + C.
What can I do to get a KeyboardInterrupt Exception that I urgently need in order to close the socket that is in use?

terminating python2 script & service with CTRL+Z

Currently whenever I press CTRL + Z on a lengthy script I was given, it immediately terminates the script ([1+] stopped(SIGTSTP) ./test.py) which is what I want, but it also leaves the python2 process running (when I type ps to look at processes), which forces me to use killall -9 python2, which I do not want to do every time. Is there a way to immediately terminate a script that doesn't leave the python2 process running in the background?
There is no SIGTSTP currently in the code that I see but I did try using the following code with no luck. It didn't even exit the script when I pressed CTRL + Z.
def handler(signum, frame):
sys.exit("CTRL+Z pressed. Exiting Test")
signal.signal(signal.SIGTSTP, handler)
SIGSTP is a signal to suspend a process, it sounds like you want to terminate a process. You can try sending Ctrl-C or CTRL-D instead, which should send a SIGINT signal.
I believe you could also try CTRL-\ which sends SIGQUIT.
Use Ctrl + C. SIGTSTP suspends the process, hence why it keeps it open, but does not terminate it.
(Note: On a Linux terminal use Ctrl + \, otherwise use Ctrl + C or Ctrl + D)
Or just use sys.exit()

Sending SIGINT to process launched from Python does not yield same result as when process is launched from shell

I am writing a script that is supposed to launch an interactive shell of a third-party program from within python in linux (the third party program is Stata). The purpose of the script is to control the stdin and stdout of the interactive shell so I can access it from an editor and other scripts.
When started from within a linux shell, Stata launches an interactive shell where you can run Stata commands. When I press control+C or use some other way to send a SIGINT signal to Stata, it will stop executing the current command and will return to the interactive shell, but it will not kill the Stata process. I want to replicate this behavior for Stata launched from python.
When I send a SIGINT signal to a Stata process launched from python, it will kill the process instead of just stopping execution of the current command.
The behavior in the shell is:
> /usr/local/stata14/stata
(...other stuff...)
. forval i = 1/100000 {
2. sleep 10
3. di "`i'"
4. }
1
2
3
4
(I hit CTRL+C)
--Break--
r(1);
.
The following python program launches Stata from within python:
def handler(signum, frame):
stata.send_signal(signal.SIGINT)
signal.signal(signal.SIGINT, handler)
stata = subprocess.Popen(["/usr/local/stata14/stata"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setpgrp)
(... other stuff hat handles forwarding stata.stdout to sys.stdout in a separate thread...)
while true:
time.sleep(0.1)
userinput = raw_input(". ")
print(userinput, file=stata.stdin)
stata.stdin.flush()
print(stata.poll())
with the same for loop running in the subprocess, produces:
> /usr/local/stata14/stata
. forval i = 1/100000 {
2. sleep 10
3. di "`i'"
4. }
1
2
3
4
-2
-2
-2
and any further attempt to write to stata.stdin then produces
print(userinput, file=stata.stdin)
IOError: [Errno 32] Broken pipe
Anyone understands why this is happening? I already tried using subprocess.Popen with shell = True. In this case, Popen launches an sh process and a Stata process. If I send SIGINT to the sh process, nothing happens. If I send it to Stata, the same problem as with shell=False arises.
Thanks in advance.

Categories