Powers74
Posts: 12
Joined: Wed Mar 25, 2020 3:03 pm

Issue when setting up a crontab entry - python script

Wed Mar 25, 2020 3:29 pm

Hi everyone,
I'm having issues with a python script (2.7). Up to several days ago, I was able to automatically launch a python script when booting up my RPI 3 Model B V1.2 (raspbian-stretch) in headless mode.

Simple crontab entry did the trick (had been working for years):

Code: Select all

@reboot /bin/sleep 20; sudo python /home/pi/Documents/PyScripts/myScript.py &
myScript.py - Infinite loop waiting for an event from user (push button callback):

Code: Select all

...
def main():
    try:
        setup_gpio()
        add_callbacks()
        show_ready()
        while(True):
            pass
            time.sleep(0.5)
    except KeyboardInterrupt:
        print("\nCtrl-C pressed")
        GPIO.cleanup()       # clean up GPIO on CTRL+C exit 
    GPIO.cleanup()

main()
Since performing an update on the system (sudo apt-get update), whenever I reboot the script it seems to hang and any sudo commands launched from a terminal goes into D+ STATUS (when using ps aux to view processes STAT column)

Any help would be appreciated - Thanks!

pcmanbob
Posts: 8392
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 11:37 am

Hi.

If all you did was sudo apt-get update all you updated was the list of files in the repository that could be update , you did not actually update any files on your OS, so nothing should have changed.

So exactly what update commands did you use ?

and does your python program actually need to run with root privileges ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Powers74
Posts: 12
Joined: Wed Mar 25, 2020 3:03 pm

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 1:15 pm

My mistake, I should have specified that I ran apt-get update && apt-get upgrade. I do need to run with root privileges as I make use of the GPIO to control electronic components (arcade buttons and 3 LED matrix panels). When I disable the crontab entry, I am able to SSH onto the Pi and run the script without any issues. While the script is running, I can open another terminal to run additional simple commands using SUDO - for example: sudo ls. Everything works fine! :D

When I re-instate the crontab entry the system boots but the script seems to get hung up. I have an initial sequence in the script (setup_gpio , add_callbacks, and show_ready) followed by a Try with a While True loop. Could it be getting stuck here? At this point when I try running the same additional commands using sudo (sudo ls) in a new terminal, it becomes unresponsive. I can still use the pi just nothing that requires sudo.

I can't even get the pi to reboot via the interface using the raspberry pi main menu; I assume that it is trying to run sudo reboot. Only option power down...

Make sense?

Cheers

User avatar
rpdom
Posts: 16398
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 1:30 pm

Powers74 wrote:
Thu Mar 26, 2020 1:15 pm
I do need to run with root privileges as I make use of the GPIO to control electronic components (arcade buttons and 3 LED matrix panels).
That is not necessarily true. Many of the GPIO functions can be used without having to be root, just by ensuring your user is a member of the correct group.

For example, group "gpio" for general GPIO usage, group "i2c" to use the access the i2c device and group "spi" to use the SPI channels.

pcmanbob
Posts: 8392
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 1:49 pm

So late in the life of stretch the upgrade command changed to sudo apt full-upgrade so the first thing to try would be to run the update again with these commands

Code: Select all

sudo apt update
sudo apt full-upgrade
this will update all packages and their dependencies.

You don't need to run python with root privileges just to use the GPIO, this changed back in the day of raspbian Jessie, and was never needed in raspbian stretch.

So try these 2 things and let us know how your program runs then.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Powers74
Posts: 12
Joined: Wed Mar 25, 2020 3:03 pm

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 2:48 pm

OK successfully completed:

Code: Select all

sudo apt update
sudo apt full-upgrade

Rebooted and tried:

Code: Select all

python /home/pi/Documents/PyScripts/myScript.py
Unsuccessful: :(
can't open /dev/mem: : Permission denied
Attempt to init outputs but initialized.
Result: 0x0 v 0xCF33070
python: framebuffer.cc:97: static void rgb_matrix::RGBMatrix::Framebuffer::InitGPIO(rgb_matrix::GPIO*): Assertion `result == b.raw' failed.
Aborted

Then tried:

Code: Select all

sudo python /home/pi/Documents/PyScripts/myScript.py
Success :D; I guess I need to run as sudo?

After, I modified the crontab:

Code: Select all

crontab -e
and added the entry below:

Code: Select all

@reboot /bin/sleep 20; sudo python /home/pi/Documents/PyScripts/myScript.py &
Rebooted: Back to the same initial problem. The script partially runs and seems to hang.

Any other suggestions?

pcmanbob
Posts: 8392
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 3:03 pm

What happens if you run it like this no sudo but with your sleep ?

Code: Select all

@reboot /bin/sleep 20;  python /home/pi/Documents/PyScripts/myScript.py  >> /home/pi/log.txt 2>&1
do you get any errors in the log.txt file ?

and you don't need the " & " at the end of a line in cron

It might also help if you showed us all of you code not just part of it.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Powers74
Posts: 12
Joined: Wed Mar 25, 2020 3:03 pm

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 3:12 pm

As instructed, I added:

Code: Select all

@reboot /bin/sleep 20;  python /home/pi/Documents/PyScripts/myScript.py  >> /home/pi/log.txt 2>&1
The log file contains the following:
can't open /dev/mem: : Permission denied
Attempt to init outputs but initialized.
python: framebuffer.cc:97: static void rgb_matrix::RGBMatrix::Framebuffer::InitGPIO(rgb_matrix::GPIO*): Assertion `result == b.raw' failed.
Result: 0x0 v 0xCF33070
Aborted

User avatar
rpdom
Posts: 16398
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 3:16 pm

It depends which GPIO functions (and libraries) you are using. Most of them should be using /dev/gpiomem for general GPIO access, which the gpio group will allow. It will not allow access to /dev/mem without sudo for security reasons.

pcmanbob
Posts: 8392
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 3:17 pm

OK so what do you get if you run it like this

Code: Select all

@reboot /bin/sleep 20; sudo python /home/pi/Documents/PyScripts/myScript.py  >> /home/pi/log.txt 2>&1
I still don't see anything in the code you posted that would require the use of sudo but from the error message I guess there is more to your code than you have posted..
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
neilgl
Posts: 1682
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 3:35 pm

I would try sudo crontab -e so it runs from root's cron not pi's. But anyway, what is actually in myScript.py?

Powers74
Posts: 12
Joined: Wed Mar 25, 2020 3:03 pm

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 3:41 pm

LOG file is empty

pcmanbob
Posts: 8392
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 3:51 pm

Powers74 wrote:
Thu Mar 26, 2020 3:41 pm
LOG file is empty
So your program does not run when run using cron , but outputs no errors !!!

If the program were crashing or hanging I would have expected an ERROR.

So how do you know your program is not running ?

can you post your entire program and no just part of it please.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Powers74
Posts: 12
Joined: Wed Mar 25, 2020 3:03 pm

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 4:03 pm

I can set it up via sudo crontab -e then reboot. My issue will then become that I can no longer modify the sudo crontab as any sudo command are unresponsive.

pcmanbob
Posts: 8392
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 4:09 pm

Powers74 wrote:
Thu Mar 26, 2020 4:03 pm
I can set it up via sudo crontab -e then reboot. My issue will then become that I can no longer modify the sudo crontab as any sudo command are unresponsive.
Then I suggest you don't do this.

I wonder if the route cause of your problem is SD card corruption ...
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Powers74
Posts: 12
Joined: Wed Mar 25, 2020 3:03 pm

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 4:14 pm

Over the last few days, I have flashed and reinstalled on 3 different SD cards - all having the same issue...

pcmanbob
Posts: 8392
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 4:20 pm

Then there must be something in your program that's causing the problem, but as you have no shown us the full program I don't see how we can help, we are now reduced to guessing

and that rarely leads to a successfully out come.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Powers74
Posts: 12
Joined: Wed Mar 25, 2020 3:03 pm

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 4:25 pm

I'll post a reduced version of it shortly so that you don't have to learn it all :D

Powers74
Posts: 12
Joined: Wed Mar 25, 2020 3:03 pm

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 5:02 pm

Here is a simplified version of the code - same issue still occurs

Code: Select all

#!/usr/bin/python

import time
import RPi.GPIO as GPIO
from rgbmatrix import Adafruit_RGBmatrix
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw

imagePath = '/home/pi/Documents/PiScripts/images/'
matrix = Adafruit_RGBmatrix(32,6)
matrixWidth = 192
matrixHeight = 32

# GPIO PIN Numbers
global button_press_pin
global button_light_pin

button_press_pin = 18
button_light_pin = 10

Button_Led_Combinations = {
        button_press_pin: button_light_pin         
        }

def setup_gpio():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(button_press_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(button_light_pin, GPIO.OUT)
	
def add_callbacks():
    GPIO.add_event_detect(button_press_pin, GPIO.FALLING, callback=callback_btn, bouncetime=600)
   
def remove_callbacks():	
    GPIO.remove_event_detect(button_press_pin)
    
def show_ready():
        lp = 0
        while lp < 5:
                for led in Button_Led_Combinations.values():
                        GPIO.output(led, True)
                        time.sleep(.15)
                        GPIO.output(led, False)
                lp += 1
        time.sleep(1)
        image = Image.open(imagePath + "splash.png") 
        image.load()
        matrix.SetImage(image.im.id, 0, 0)
        time.sleep(3)
        matrix.Clear()

def clear_lights():
    GPIO.output(button_light_pin, False)

def callback_btn(channel):

	clear_lights()
	GPIO.output(button_light_pin, True)
        
        image = Image.open(imagePath + "splash.png") 
        image.load()
        matrix.SetImage(image.im.id, 0, 0)
        time.sleep(3)
        matrix.Clear()
	time.sleep(0.5)
	clear_lights()
	                          
def main():
    try:
        setup_gpio()
        add_callbacks()
        show_ready()
        while(True):
            pass
            time.sleep(0.5)
    except KeyboardInterrupt:
        print("\nCtrl-C pressed")
        GPIO.cleanup()       # clean up GPIO on CTRL+C exit 
    GPIO.cleanup()

main()

Powers74
Posts: 12
Joined: Wed Mar 25, 2020 3:03 pm

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 5:12 pm

Running processes (ps aux) - 3rd line D+ ???

root 948 0.0 0.3 7220 3216 ? S 12:56 0:00 sudo python /home/pi/Documents/PiScripts/myScript.py
root 952 99.2 1.0 33300 9876 ? Sl 12:56 1:20 python /home/pi/pi/Documents/PiScripts/myScript.py
root 977 0.0 0.2 5500 2540 pts/0 D+ 12:56 0:00 sudo ls
pi 979 3.0 0.4 6108 4012 pts/1 Ss 12:57 0:00 bash

User avatar
Paeryn
Posts: 2841
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Issue when setting up a crontab entry - python script

Thu Mar 26, 2020 7:26 pm

Powers74 wrote:
Thu Mar 26, 2020 5:12 pm
Running processes (ps aux) - 3rd line D+ ???

root 948 0.0 0.3 7220 3216 ? S 12:56 0:00 sudo python /home/pi/Documents/PiScripts/myScript.py
root 952 99.2 1.0 33300 9876 ? Sl 12:56 1:20 python /home/pi/pi/Documents/PiScripts/myScript.py
root 977 0.0 0.2 5500 2540 pts/0 D+ 12:56 0:00 sudo ls
pi 979 3.0 0.4 6108 4012 pts/1 Ss 12:57 0:00 bash
The status D says that the process is in uninterruptable sleep, usually because the process made a system call and the kernel itself is waiting for something (typically IO) to complete before it returns.
What happens if, when it's stuck like above, you send a SIGINT to the python process which should, according to your program, gracefully terminate. Hopefully that will allow the sudo running it to end. Just wondering if somehow that sudo python is locking sudo ls out, not that it should.

Substitute python's process i.d. with whatever it is, 952 is what it was on the run you showed. -2 says to send it SIGINT (a.k.a. Ctrl-C)

Code: Select all

kill -2 952
She who travels light — forgot something.

User avatar
neilgl
Posts: 1682
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Issue when setting up a crontab entry - python script

Fri Mar 27, 2020 11:12 am

Can you let us know which RGB Matrix is attached and is it using a HAT or other connection?

Powers74
Posts: 12
Joined: Wed Mar 25, 2020 3:03 pm

Re: Issue when setting up a crontab entry - python script

Fri Mar 27, 2020 12:18 pm

Works fine when SSH into the Pi and launched from a terminal. The issue is when trying to set it up to launch when Pi is rebooted via crontab.

Final product: https://github.com/TooMuchPowers74/NHL_ ... hots/1.JPG

Using the following:
Adafruit RGB Matrix HAT + RTC for Raspberry Pi: https://www.adafruit.com/product/2345
3 chained RGB LED Matrix - 4mm pitch: https://www.adafruit.com/product/2278

Garvan
Posts: 41
Joined: Sun Jan 05, 2020 9:59 am

Re: Issue when setting up a crontab entry - python script

Sat Mar 28, 2020 10:36 am

Two days, and no progress. I think you will have to solve this issue yourself.

I would start by inserting debug code everyplace (logging the results of every single line of code to file), or make a simple basic script that does nothing, and add functionality line by line until you identify what fails. Has the permissions on (imagePath + "splash.png) changed during your upgrade? Try commenting this out to see if it still fails.

Good luck.

Powers74
Posts: 12
Joined: Wed Mar 25, 2020 3:03 pm

Re: Issue when setting up a crontab entry - python script

Wed Apr 01, 2020 11:20 pm

It ended up being the rgbmatrix library. Made a modification to 1 of the modules; Thread.cc

Details can be found here:
https://raspberrypi.stackexchange.com/q ... om-running

Thanks to everyone for helping out!
:D

Return to “Python”