ZXX3N
Posts: 7
Joined: Sun Oct 13, 2019 11:08 pm

Raspberry Pi 3 Crashes after 20 mins of python script running

Sun Oct 13, 2019 11:23 pm

So i have a script that I made that detects when a switch is moved on my raspberry pi 3, then sends a letter to my computer to press. But for some reason after some time of running the script it just crashes and Putty says that software on the pi closed the connection and the script stops. I cant figure out why it does this and since the ssh session crashes I don't see any error from the script. The session just freezes then when I click it or attempt to control-c the connection crashes

My Code

Code: Select all

import RPi.GPIO as GPIO
import time
import socket

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)



GPIO.setmode(GPIO.BCM)


GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)

LastInput0 = 0
LastInput1 = 0
LastInput2 = 0
LastInput3 = 0
LastInput4 = 0
LastInput5 = 0
LastInput6 = 0
LastInput7 = 0
LastInput8 = 0
LastInput9 = 0

client.connect(('10.0.0.67', 8080))

while True:
        input_state0 = GPIO.input(18)
        if input_state0 == False and LastInput0 == 1:
                client.send("o")
                from_server = client.recv(4096)
                print from_server
                time.sleep(.2)
                LastInput0 = 0
        if input_state0 == True and LastInput0 == 0:
                client.send("o")
                print("nothing done")
                time.sleep(.2)
                LastInput0 = 1
        input_state1 = GPIO.input(23)
        if input_state1 == False and LastInput1 == 1:
                client.send("i")
                from_server = client.recv(4096)
                print from_server
                LastInput1 = 0
                time.sleep(.2)
        if input_state1 == True and LastInput1 == 0:
                client.send("i")
                from_server = client.recv(4096)
                print from_server
                LastInput1 = 1
                time.sleep(.2)
        input_state2 = GPIO.input(24)
        if input_state2 == False and LastInput2 == 1:
                client.send("u")
                from_server = client.recv(4096)
                print from_server
                LastInput2 = 0
                time.sleep(.2)
        if input_state2 == True and LastInput2 == 0:
                client.send("u")
                from_server = client.recv(4096)
                print from_server
                LastInput2 = 1
                time.sleep(.2)
        input_state3 = GPIO.input(14)
        if input_state3 == False and LastInput3 == 1:
                client.send(",")
                from_server = client.recv(4096)
                print from_server
                LastInput3 = 0
                time.sleep(.2)
        if input_state3 == True and LastInput3 == 0:
                client.send(",")
                from_server = client.recv(4096)
                print from_server
                LastInput3 = 1
                time.sleep(.2)
                        input_state4 = GPIO.input(15)
        if input_state4 == False and LastInput4 == 1:
                client.send(".")
                from_server = client.recv(4096)
                print from_server
                LastInput4 = 0
                time.sleep(.2)
        if input_state4 == True and LastInput4 == 0:
                client.send(".")
                from_server = client.recv(4096)
                print from_server
                LastInput4 = 1
                time.sleep(.2)
        input_state5 = GPIO.input(25)
        if input_state5 == False and LastInput5 == 1:
                client.send("/")
                from_server = client.recv(4069)
                LastInput5 = 0
                time.sleep(.2)
        if input_state5 == True and LastInput5 == 0:
                client.send("/")
                from_server = client.recv(4069)
                LastInput5 = 1
                time.sleep(.2)
        input_state6 = GPIO.input(4)
        if input_state6 == False and LastInput6 == 1:
                client.send("b")
                from_server = client.recv(4096)
                print from_server
                LastInput6 = 0
                time.sleep(.2)
        if input_state6 == True and LastInput6 == 0:
                client.send("b")
                from_server = client.recv(4069)
                print from_server
                LastInput6 = 1
                time.sleep(.2)
        input_state7 = GPIO.input(17)
        if input_state7 == False and LastInput7 == 1:
                client.send("v")
                from_server = client.recv(4096)
                print from_server
                LastInput7 = 0
                time.sleep(.2)
        if input_state7 == True and LastInput7 == 0:
                client.send("v")
                from_server = client.recv(4096)
                print from_server
                LastInput7 = 1
                time.sleep(.2)
        input_state8 = GPIO.input(27)
        if input_state8 == False and LastInput8 == 1:
                client.send("j")
                from_server = client.recv(4096)
                print from_server
                LastInput8 = 0
                time.sleep(.2)
        if input_state8 == True and LastInput8 == 0:
                client.send("j")
                from_server = client.recv(4096)
                print from_server
                LastInput8 = 1
                time.sleep(.2)
                        input_state9  = GPIO.input(22)
        if input_state9 == False and LastInput9 == 1:
                client.send("h")
                from_server = client.recv(4096)
                print from_server
                LastInput9 =  0
                time.sleep(.2)
        if input_state9 == True and LastInput9 == 0:
                client.send("h")
                from_server = client.recv(4096)
                print from_server
                LastInput9 = 1
                time.sleep(.2)
 

User avatar
B.Goode
Posts: 8898
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Raspberry Pi 3 Crashes after 20 mins of python script running

Mon Oct 14, 2019 5:24 pm

Welcome to the Raspberry Pi forums.


Some debugging suggestions -

Fix the unexpected indentation relating to state4 and state9.

Customise the print statements so you have a record of what route has been taken through the script.

When you run the script, redirect the output to a file so that you have a permanent record of the output if a crash occurs.

Reduce the number of inputs to, say, 2 or 4, and see if the behaviour of the code changes. If it runs for longer, gradually increase the number until it fails.



And finally, although not strictly debugging or troubleshooting, be aware that the Python2 dialect of the Python language reaches end of life soon.

ZXX3N
Posts: 7
Joined: Sun Oct 13, 2019 11:08 pm

Re: Raspberry Pi 3 Crashes after 20 mins of python script running

Mon Oct 14, 2019 6:12 pm

The unexpected indentation in the code that i posted isnt there in the script on the pi. I must have accidentally put it there while copying from the terminal to here. Sorry about that.


I am at school right now and will costomize the print statements, but part of the server script is sending back the key that got pressed to make sure that it got sent and received right.

I am at at school at the minute and will do that when I get home and update you on what happens.

I will also try reducing the outputs of the script to around 2 and do that and update about the outcome of that.

I am a novice to python and dont really know what parts of my script are Python 2 I know that I have Python 3 installed. What does that mean the python 2 dialect will be dead soon?

User avatar
B.Goode
Posts: 8898
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Raspberry Pi 3 Crashes after 20 mins of python script running

Mon Oct 14, 2019 6:14 pm

An afterthought.

Is your RPi running stable in the absence of your own script?

If you connect via SSH and run something else, like top, is the system reliable and long-running?

User avatar
rpiMike
Posts: 962
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Raspberry Pi 3 Crashes after 20 mins of python script running

Mon Oct 14, 2019 9:34 pm

Unless one of your switches is pressed there doesn't appear to be a sleep in your 'while true' loop. This will cause high cpu usage - add a small 'time.sleep(0.01)' at the end of your loop.

ZXX3N
Posts: 7
Joined: Sun Oct 13, 2019 11:08 pm

Re: Raspberry Pi 3 Crashes after 20 mins of python script running

Tue Oct 15, 2019 12:04 am

B.Goode wrote:
Mon Oct 14, 2019 6:14 pm
An afterthought.

Is your RPi running stable in the absence of your own script?

If you connect via SSH and run something else, like top, is the system reliable and long-running?
The pi runs fine for hours if i dont run the script. So it must be the script

ZXX3N
Posts: 7
Joined: Sun Oct 13, 2019 11:08 pm

Re: Raspberry Pi 3 Crashes after 20 mins of python script running

Tue Oct 15, 2019 6:14 pm

rpiMike wrote:
Mon Oct 14, 2019 9:34 pm
Unless one of your switches is pressed there doesn't appear to be a sleep in your 'while true' loop. This will cause high cpu usage - add a small 'time.sleep(0.01)' at the end of your loop.
I added this to the end of my loop and I ran it and it ran until I went to sleep but when I woke up I checked on it and it had crashed.

ZXX3N
Posts: 7
Joined: Sun Oct 13, 2019 11:08 pm

Re: Raspberry Pi 3 Crashes after 20 mins of python script running

Tue Oct 15, 2019 10:47 pm

could this be a problem with the raspberry pi going to sleep, im wondering this because it freezes then crashes my ssh session

User avatar
DougieLawson
Posts: 36327
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Raspberry Pi 3 Crashes after 20 mins of python script running

Tue Oct 15, 2019 11:38 pm

ZXX3N wrote:
Tue Oct 15, 2019 10:47 pm
could this be a problem with the raspberry pi going to sleep, im wondering this because it freezes then crashes my ssh session
There's no "sleep" mode on any Raspberry Pi running Raspbian.
The crash is bound to be a bug in your code.

Try running a free command from cron every minute, logging the results to /var/log. That should show that you've got a memory leak (which is the usual cause of these problems when you've eliminated a weedy power supply).
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
Gavinmc42
Posts: 3927
Joined: Wed Aug 28, 2013 3:31 am

Re: Raspberry Pi 3 Crashes after 20 mins of python script running

Wed Oct 16, 2019 2:08 am

I had Python software loop memory issues year ago.
Never bothered to find out why, I used free to watch memory disappear.
Found other ways that worked instead.
Now I tend to use watch or cron for datalog triggering.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

User avatar
B.Goode
Posts: 8898
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Raspberry Pi 3 Crashes after 20 mins of python script running

Wed Oct 16, 2019 6:35 am

ZXX3N wrote:
Tue Oct 15, 2019 10:47 pm
could this be a problem with the raspberry pi going to sleep, im wondering this because it freezes then crashes my ssh session



You know that isn't the case, because you previously posted -
The pi runs fine for hours if i dont run the script. So it must be the script

User avatar
rpiMike
Posts: 962
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Raspberry Pi 3 Crashes after 20 mins of python script running

Wed Oct 16, 2019 6:53 am

ZXX3N wrote:
Tue Oct 15, 2019 6:14 pm
rpiMike wrote:
Mon Oct 14, 2019 9:34 pm
Unless one of your switches is pressed there doesn't appear to be a sleep in your 'while true' loop. This will cause high cpu usage - add a small 'time.sleep(0.01)' at the end of your loop.
I added this to the end of my loop and I ran it and it ran until I went to sleep but when I woke up I checked on it and it had crashed.
Did cpu usage reduce with the sleep added? Re-post your code.

ZXX3N
Posts: 7
Joined: Sun Oct 13, 2019 11:08 pm

Re: Raspberry Pi 3 Crashes after 20 mins of python script running

Wed Oct 16, 2019 6:16 pm

Adding the sleeo at the end of the loop brought the cpu usage down to 0. But I also noticed that after about 10 minutes the ssh session freezes and when I press something it takes around 10 seconds before it comes back to life.

Code: Select all

import RPi.GPIO as GPIO
import time
import socket

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)



GPIO.setmode(GPIO.BCM)


GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)

LastInput0 = 0
LastInput1 = 0
LastInput2 = 0
LastInput3 = 0
LastInput4 = 0
LastInput5 = 0
LastInput6 = 0
LastInput7 = 0
LastInput8 = 0
LastInput9 = 0

client.connect(('10.0.0.67', 8080))

while True:
        input_state0 = GPIO.input(18)
        if input_state0 == False and LastInput0 == 1:
                client.send("o")
                from_server = client.recv(4096)
                print(from_server + "o")
                time.sleep(.2)
                LastInput0 = 0
        if input_state0 == True and LastInput0 == 0:
                client.send("o")
                print("nothing done")
                time.sleep(.2)
                LastInput0 = 1
        input_state1 = GPIO.input(23)
        if input_state1 == False and LastInput1 == 1:
                client.send("i")
                from_server = client.recv(4096)
                print(from_server)
                LastInput1 = 0
                time.sleep(.2)
	if input_state1 == True and LastInput1 == 0:
		client.send("i")
		from_server = client.recv(4096)
		print(from_server)
		LastInput1 = 1
		time.sleep(.2)
        input_state2 = GPIO.input(24)
        if input_state2 == False and LastInput2 == 1:
                client.send("u")
                from_server = client.recv(4096)
                print(from_server)
                LastInput2 = 0
                time.sleep(.2)
        if input_state2 == True and LastInput2 == 0:
                client.send("u")
                from_server = client.recv(4096)
                print(from_server)
                LastInput2 = 1
                time.sleep(.2)
        input_state3 = GPIO.input(14)
        if input_state3 == False and LastInput3 == 1:
                client.send(",")
                from_server = client.recv(4096)
                print(from_server)
                LastInput3 = 0
                time.sleep(.2)
        if input_state3 == True and LastInput3 == 0:
		client.send(",")
                from_server = client.recv(4096)
                print(from_server)
                LastInput3 = 1
                time.sleep(.2)
        input_state4 = GPIO.input(15)
        if input_state4 == False and LastInput4 == 1:
                client.send(".")
                from_server = client.recv(4096)
                print(from_server)
                LastInput4 = 0
                time.sleep(.2)
	if input_state4 == True and LastInput4 == 0:
		client.send(".")
		from_server = client.recv(4096)
		print(from_server)
		LastInput4 = 1
		time.sleep(.2)
        input_state5 = GPIO.input(25)
	if input_state5 == False and LastInput5 == 1:
	        client.send("/")
             	from_server = client.recv(4069)
                LastInput5 = 0
                time.sleep(.2)
        if input_state5 == True and LastInput5 == 0:
                client.send("/")
                from_server = client.recv(4069)
                LastInput5 = 1
                time.sleep(.2)
        input_state6 = GPIO.input(4)
        if input_state6 == False and LastInput6 == 1:
		client.send("b")
		from_server = client.recv(4096)
		print(from_server)
		LastInput6 = 0
		time.sleep(.2)
	if input_state6 == True and LastInput6 == 0:
	        client.send("b")
		from_server = client.recv(4069)
		print(from_server)
		LastInput6 = 1
		time.sleep(.2)
	input_state7 = GPIO.input(17)
	if input_state7 == False and LastInput7 == 1:
	        client.send("v")
		from_server = client.recv(4096)
		print(from_server)
		LastInput7 = 0
		time.sleep(.2)
	if input_state7 == True and LastInput7 == 0:
	        client.send("v")
		from_server = client.recv(4096)
		print(from_server)
		LastInput7 = 1
		time.sleep(.2)
	input_state8 = GPIO.input(27)
	if input_state8 == False and LastInput8 == 1:
	       	client.send("j")
		from_server = client.recv(4096)
		print(from_server)
		LastInput8 = 0
		time.sleep(.2)
	if input_state8 == True and LastInput8 == 0:
		client.send("j")
		from_server = client.recv(4096)
		print(from_server)
		LastInput8 = 1
		time.sleep(.2)
	input_state9  = GPIO.input(22)
	if input_state9 == False and LastInput9 == 1:
		client.send("h")
		from_server = client.recv(4096)
		print(from_server)
		LastInput9 =  0
		time.sleep(.2)
	if input_state9 == True and LastInput9 == 0:
		client.send("h")
		from_server = client.recv(4096)
		print(from_server)
		LastInput9 = 1
		time.sleep(.2)
	time.sleep(.2)


User avatar
rpiMike
Posts: 962
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Raspberry Pi 3 Crashes after 20 mins of python script running

Wed Oct 16, 2019 8:13 pm

Maybe its just ssh or network problems - not your code.

Have you got a keyboard and monitor/tv you can use to test your code directly on your Pi without ssh.

Alternatively configure your Pi to start your code at boot up.

User avatar
B.Goode
Posts: 8898
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Raspberry Pi 3 Crashes after 20 mins of python script running

Wed Oct 16, 2019 8:48 pm

rpiMike wrote:
Wed Oct 16, 2019 8:13 pm
Maybe its just ssh or network problems - not your code.

Have you got a keyboard and monitor/tv you can use to test your code directly on your Pi without ssh.

Alternatively configure your Pi to start your code at boot up.


See https://www.raspberrypi.org/forums/view ... 4#p1551894

I believe that has been dealt with?

User avatar
rpiMike
Posts: 962
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Raspberry Pi 3 Crashes after 20 mins of python script running

Wed Oct 16, 2019 9:03 pm

B.Goode wrote:
Wed Oct 16, 2019 8:48 pm
rpiMike wrote:
Wed Oct 16, 2019 8:13 pm
Maybe its just ssh or network problems - not your code.

Have you got a keyboard and monitor/tv you can use to test your code directly on your Pi without ssh.

Alternatively configure your Pi to start your code at boot up.


See https://www.raspberrypi.org/forums/view ... 4#p1551894

I believe that has been dealt with?
Is that runs for hours with ssh connected?

ZXX3N
Posts: 7
Joined: Sun Oct 13, 2019 11:08 pm

Re: Raspberry Pi 3 Crashes after 20 mins of python script running

Wed Oct 16, 2019 11:50 pm

DougieLawson wrote:
Tue Oct 15, 2019 11:38 pm
ZXX3N wrote:
Tue Oct 15, 2019 10:47 pm
could this be a problem with the raspberry pi going to sleep, im wondering this because it freezes then crashes my ssh session
There's no "sleep" mode on any Raspberry Pi running Raspbian.
The crash is bound to be a bug in your code.

Try running a free command from cron every minute, logging the results to /var/log. That should show that you've got a memory leak (which is the usual cause of these problems when you've eliminated a weedy power supply).
I added the free command to my cron and im testing it now overnight is there a correct way to add this i just added it as * * * * * free in my cron page is that wrong

Return to “Troubleshooting”