PinkusJ
Posts: 1
Joined: Sun May 09, 2021 7:39 pm

RPIO.GPIO does not work when ran from crontab

Sun May 09, 2021 7:49 pm

When I try running a simple script involving the RPi.GPIO module from Thonny IDE or the command line, it works just fine. However, when I try to run it from a crontab, it gives the following error:

Code: Select all

Traceback (most recent call last):
  File "/home/pi/Desktop/CanSat/Code/master.py", line 2, in <module>
    import RPi.GPIO as GPIO
  File "/usr/local/lib/python3.7/dist-packages/RPi/GPIO/__init__.py", line 23, in <module>
    from RPi._GPIO import *
RuntimeError: This module can only be run on a Raspberry Pi!
I edited the crontab via "sudo crontab -e" and the crontab instructions are:

Code: Select all

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
@reboot sleep 30  &&  python3 /path/to/file.py >> /path/to/error_log/ 2>&1
Any ideas as to why this might occur? I tried similar online questions and none of them seemed to work in this case!

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

Re: RPIO.GPIO does not work when ran from crontab

Mon May 10, 2021 2:14 am

I think I remember crontab being very picky about paths, you need to put in the full path name for the file.
"/home pi/....py"
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

MiscBits
Posts: 249
Joined: Wed Jan 27, 2021 12:48 pm

Re: RPIO.GPIO does not work when ran from crontab

Mon May 10, 2021 3:20 am

This is incorrect:

Code: Select all

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
@reboot sleep 30  &&  python3 /path/to/file.py >> /path/to/error_log/ 2>&1
Cron jobs must start with a time structure and not the PATH option.

Unless you where running the command line version under SUDO, then you should be using the user crontab not the root one.

The 'RuntimeError: This module can only be run on a Raspberry Pi!' error if often seen when using older versions - it's possible that the root version and user version is different. Did you load one as sudo and one as the normal user?

I normally create a short shell script to set up any environment and call that from cron if needed and in this case it would be the path, sleep and python commands.
Is a computer language with goto's totally Wirth-less?

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

Re: RPIO.GPIO does not work when ran from crontab

Mon May 10, 2021 3:35 am

Code: Select all

SHELL=/bin/sh
PATH=/sbin/:/bin:/usr/sbin:/usr/bin



* * * * * TZ=EST-10 /home/tc/fridgealarm.sh >> /var/log/error.log
This is what I have in one of mine that has been running for years.
That is the default for running every minute.
By default it uses UTC, that why the timezone is in there.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

SurferTim
Posts: 2054
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: RPIO.GPIO does not work when ran from crontab

Tue May 11, 2021 9:45 am

The last time I saw this message was when the user was not a member of the gpio group.

Code: Select all

RuntimeError: This module can only be run on a Raspberry Pi!
I haven't used crontab in a while so I'm not certain what user it runs as. On the RPi, it appears user root is not a member of the gpio group.

Code: Select all

sudo groups
You can apparently assign the user with this. I haven't tried it.

Code: Select all

sudo crontab -u pi -e
My advice applies to RaspiOS only. Please mention if you use another OS.

User avatar
neilgl
Posts: 3249
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: RPIO.GPIO does not work when ran from crontab

Tue May 11, 2021 10:42 am

You want to use the pi user crontab via crontab -e, not root’s crontab via sudo crontab -e
This works for me in pi crontab:

Code: Select all

@reboot python3 -u /home/pi/scripts/neo.py >> /home/pi/scripts/neo.log 2>&1

Return to “Python”