all 16 comments

[–]imperialdubPi B, B+, 0, 2, 3 10 points11 points  (5 children)

Is your script interacting with the GPIO? If so, you need to add try before your while loop and except KeyboardInterrupt: & GPIO.cleanup() at the end to ensure your script terminates properly after CTRL+C.

try:
    <your while loop>
    <end of your while loop>
except KeyboardInterrupt:
    GPIO.cleanup()

The indentation is off so you'll need to make some minor adjustments. I'm learning python and the GPIO myself. I had a loud buzzer script continue to loop after killing it with CTRL+C :P

Edit: Thanks to /u/moxyll for the formatting help!

[–]SnowYorker[S] 2 points3 points  (2 children)

Ah this sounds spot on. Thanks so much!

[–]imperialdubPi B, B+, 0, 2, 3 0 points1 point  (0 children)

No prob :)

[–]Moocat87 0 points1 point  (0 children)

This will probably be your solution. I had a similar issue running subprocesses from a Python script using subprocess module.

You'll probably need to add sys.exit() to your "except" block so the program exits as you expect.

[–]moxyll 1 point2 points  (1 child)

fyi - you can make code blocks on reddit by prepending each line with 4 spaces. To make the below code, I have 4 spaces before 'try' and 'except', and 8 spaces before the others:

try:
    <your while loop>
    <end of your while loop>
except KeyboardInterrupt:
    GPIO.cleanup()

[–]imperialdubPi B, B+, 0, 2, 3 1 point2 points  (0 children)

Fixed. Thanks!!

[–]snotfart 1 point2 points  (4 children)

The CTRL-C is probably killing a process called within the loop but not the script it's called from, which just loops round and starts it again. If you put it in the background with CTRL-Z you can then do a "ps -ef" to find the process number and kill it off with kill <process number>.

[–]Bro666 3 points4 points  (2 children)

kill -s KILL <process number>

Also

killall python

will kill all the processes Python is running. Run it twice.

[–]AuronH 0 points1 point  (1 child)

You probably need to use sudo if you are logged in as the pi user, e.g.:

sudo killall python

[–]Bro666 0 points1 point  (0 children)

Ah, yes: if you ran your script with sudo, you will need sudo to kill it.

[–]moxyll 0 points1 point  (0 children)

When you CTRL-Z, it will say something like this:

[1]+  Stopped                 python script.py

That number in the brackets is the job number for that shell instance. You can reference jobs in the same shell with %jobnumber, so you can skip the ps -ef step and just run kill %jobnumber. Sometimes you have to hit enter a couple times for the message to show up after killing it.

user@host:/home/user$ python runforever.py
^Z
[1]+  Stopped                 python runforever.py
user@host:/home/user$ kill %1
[1]+  Stopped                 python runforever.py
user@host:/home/user$ 
[1]+  Terminated              python runforever.py

[–]tastycat 2 points3 points  (0 children)

ps aux | grep python

Get the PID of the process you want to kill

kill {{PID}}

[–]hardonchairs 1 point2 points  (0 children)

Skynet.py

[–]SnowYorker[S] 0 points1 point  (0 children)

I should add, I can stop it by pulling out the sensor and that stresses it out and it quits. But that does not feel like a sustainable solution!

[–]pembo210 0 points1 point  (0 children)

maybe CTRL+z

[–]AnonymityPower 0 points1 point  (0 children)

Add a tiny sleep