Viper1953
Posts: 19
Joined: Tue Dec 31, 2013 10:09 am

Using CTRL_C_EVENT on button press

Wed Feb 26, 2014 7:49 am

Hi gang,

This issue has been driving me nuts over the past few days. I have a script that I wish to terminate upon the press of a button.

Currently using try/except with KeyboardInterrupt which works fantastically if I actually press ctrl+c on the keyboard, but ultimately the script will be running headless so need the button input.

so far I have got this:

Code: Select all

def killScript(pin):
        os.kill(signal.CTRL_C_EVENT)

GPIO.add_event_detect(22, GPIO.RISING, callback=killScript, bouncetime=200)

here is the traceback:
Traceback (most recent call last):
File "/home/pi/scripts/picam/picam.py", line 50, in killScript
os.kill(signal.CTRL_C_EVENT)
AttributeError: 'module' object has no attribute 'CTRL_C_EVENT'

I'm open to other methods if I've got it all wrapped around my brain and confused!

I don't fully understand signals yet, so any complex terminology may go straight over my head!

Thanks in advance

Mark

PiGraham
Posts: 3971
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Using CTRL_C_EVENT on button press

Wed Feb 26, 2014 8:36 am

Python docs wrote:Availability: Windows.
Try signal.SIGINT

Viper1953
Posts: 19
Joined: Tue Dec 31, 2013 10:09 am

Re: Using CTRL_C_EVENT on button press

Wed Feb 26, 2014 8:41 am

Hi Graham,

I did try that earlier, and again just now and got the following:

Traceback (most recent call last):
File "/home/pi/scripts/picam/picam.py", line 50, in killScript
os.kill(signal.SIGINT)
TypeError: kill() takes exactly 2 arguments (1 given)

PiGraham
Posts: 3971
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Using CTRL_C_EVENT on button press

Wed Feb 26, 2014 9:00 am

kill does indeed take two arguments - the PID of the process you want to send a signal to, and the signal to send.

I'm not a Python expert.
You can use subprocess to launch other programs, and use the returned object to send signals (the subprocess object knows the PID).

You can get the PID of any process via
These links may help:
http://bytes.com/topic/python/answers/8 ... ng-cntrl-c
http://stackoverflow.com/questions/2021 ... -os-system

You can also use os-system to execute pkill or killall which accept a process name instead of a PID

I think subprocess is the preferred way to control other programs.

User avatar
jojopi
Posts: 3274
Joined: Tue Oct 11, 2011 8:38 pm

Re: Using CTRL_C_EVENT on button press

Wed Feb 26, 2014 11:43 am

Code: Select all

os.kill(os.getpid(), signal.SIGINT)
But more simply and portably:

Code: Select all

raise(KeyboardInterrupt)

PiGraham
Posts: 3971
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Using CTRL_C_EVENT on button press

Wed Feb 26, 2014 12:50 pm

Oh yes, the OP wants to quit the current process.

I was thinking of the case where you want to end another process that has been spawned by this one.

Is Py_Exit(int) a good option for the OP?

Viper1953
Posts: 19
Joined: Tue Dec 31, 2013 10:09 am

Re: Using CTRL_C_EVENT on button press

Wed Feb 26, 2014 3:10 pm

Hi guys,

Apologies for the lateness in replying - had to nip out.

This did it for me:
jojopi wrote:

Code: Select all

os.kill(os.getpid(), signal.SIGINT)
For some reason I had already tried os.getpid() but obviously it was either in the wrong place or there were other factors which I had written incorrectly.

When the camera is running, there is some fancy output to the oLED screen but when a Keyboard Interupt is triggered it switches to a 'summary' screen of activity (status of camera(off), number of captures etc). So it was kind of imperitive that I got this working with a button.

Once I have the script in a more orderly state I'll post up some more info etc.

Once again, many thanks for your replies!

Return to “Python”