A small, affordable computer with free resources to help people learn, make things, and have fun
https://forums.raspberrypi.com/
Is this Python program set up to run in a Virtual Environment (VENV) or stand-alone. If the latter, then you should be able to run it from a command line and/or using the above link to run at start up.However, I cannot get any joy when trying to create a crontab job, that will run a Python program on a Raspberry Pi Model 3B+ running bookworm.
Code: Select all
#!/bin/sh
export XDG_RUNTIME_DIR="/run/user/$(id -u)"
export DISPLAY="$(echo $DISPLAY)"
/your/py/script
kerry_s wrote: ↑Tue Oct 14, 2025 9:06 pmfor cron you need to do a little script
Code: Select all
#!/bin/sh export XDG_RUNTIME_DIR="/run/user/$(id -u)" export DISPLAY="$(echo $DISPLAY)" /your/py/script
Code: Select all
import time
import datetime as dt
from picamera2 import Picamera2, Preview
picam2 = Picamera2()
picam2.start_preview(Preview.QT, x=200, y=200, width=400, height=300)
picam2.start() # this line starts the preview
filename = dt.datetime.now().strftime('%A %d %b %Y %H:%M')
picam2.capture_file('/home/pi/Desktop/' + str(filename) + '.jpg')
picam2.capture_file('/home/pi/Pictures/' + str(filename) + '.jpg')Can you run the program manually, outside of CRON successfully e.g.As stated in my OP, I have run a similar program for years using an older Pi but something must have changed to prevent it working on a later Pi, running bookworm.
Code: Select all
$ python3 /home/pi/Desktop/myprog.py
Yes, I can, it works as expected. Thanks, I was not aware you could run a program like that.DS256 wrote: ↑Wed Oct 15, 2025 1:06 pmCan you run the program manually, outside of CRON successfully e.g.As stated in my OP, I have run a similar program for years using an older Pi but something must have changed to prevent it working on a later Pi, running bookworm.
Code: Select all
$ python3 /home/pi/Desktop/myprog.py
RDS wrote: ↑Wed Oct 15, 2025 1:20 pmYes, I can, it works as expected. Thanks, I was not aware you could run a program like that.DS256 wrote: ↑Wed Oct 15, 2025 1:06 pmCan you run the program manually, outside of CRON successfully e.g.As stated in my OP, I have run a similar program for years using an older Pi but something must have changed to prevent it working on a later Pi, running bookworm.
Code: Select all
$ python3 /home/pi/Desktop/myprog.py
(I thought you had found the problem because I always quote Python 3, rather than Python3 but I have it correct in the cron line)
thagrol wrote: ↑Wed Oct 15, 2025 2:24 pmRDS wrote: ↑Wed Oct 15, 2025 1:20 pmYes, I can, it works as expected. Thanks, I was not aware you could run a program like that.DS256 wrote: ↑Wed Oct 15, 2025 1:06 pm
Can you run the program manually, outside of CRON successfully e.g.
Code: Select all
$ python3 /home/pi/Desktop/myprog.py
(I thought you had found the problem because I always quote Python 3, rather than Python3 but I have it correct in the cron line)
That tells us very little about whether it will run under cron. Many things are different under a logged in shell and under cron (see section 3.5 of my guide).
I'm repeating myself here but, untill you post the error message(s) all anyone can do is guess. Try changing your crontab entry to two minutes from now rather than waiting until 22:00.
Please post your CRONTAB definitionsIn section 6.1, I ran the second error finding option (grep .... ) and this resulted in a 'No such file or directory' error
Code: Select all
crontab -lThat is an example based on the examples shown previously. You are suppose to add the 2> to your command.I could not understand the @reboot ping google.co. 2>$HOME/ping.log code or where it should go. What has google got to do with it?
RDS wrote: ↑Wed Oct 15, 2025 8:36 pmthagrol wrote: ↑Wed Oct 15, 2025 2:24 pm
That tells us very little about whether it will run under cron. Many things are different under a logged in shell and under cron (see section 3.5 of my guide).
I'm repeating myself here but, untill you post the error message(s) all anyone can do is guess. Try changing your crontab entry to two minutes from now rather than waiting until 22:00.
Thanks. I had looked at your guide.
In section 6.1, I ran the second error finding option (grep .... ) and this resulted in a 'No such file or directory' error but I could not understand the @reboot ping google.co. 2>$HOME/ping.log code or where it should go. What has google got to do with it?
Thank you for your patience. First of all though, I certainly would not ask for an update to your guide.thagrol wrote: ↑Wed Oct 15, 2025 10:45 pmRDS wrote: ↑Wed Oct 15, 2025 8:36 pmthagrol wrote: ↑Wed Oct 15, 2025 2:24 pm
That tells us very little about whether it will run under cron. Many things are different under a logged in shell and under cron (see section 3.5 of my guide).
I'm repeating myself here but, untill you post the error message(s) all anyone can do is guess. Try changing your crontab entry to two minutes from now rather than waiting until 22:00.
Thanks. I had looked at your guide.
In section 6.1, I ran the second error finding option (grep .... ) and this resulted in a 'No such file or directory' error but I could not understand the @reboot ping google.co. 2>$HOME/ping.log code or where it should go. What has google got to do with it?
Nothing. As ame said, that's an example (that's what e.g. in the sentence above meant*).
As for searching /var/log/syslog, that won't work on Trixie. There is no syslog as everything goes to the systemd journal. See man journalctl for help with that.
And before you ask, no, there won't be an update to the gudie for that. See EOLing My Guides
*: No, not literally. That's "exempli gratia"
RDS wrote: ↑Thu Oct 16, 2025 9:44 pmThank you for your patience. First of all though, I certainly would not ask for an update to your guide.
I have spent time on this today and I am getting nowhere. I cannot find any cron related errors.
From Section 6.1 in your guide when entering:
@reboot ping /home/pi/Desktop/myprog.py 2>$HOME/ping.log
it returns:
bash: @reboot: command not found
in the ping.log file
Further questions/points if I may
1) Does the pi need to be rebooted after any change to the crontab file?
2) How do I know which crontab file to use (e.g. sudo crontab -e or just crontab -e or is there another one)
3) Entering the grep line option from Section 6.1 results in no such file or directory being returned
4) I cannot find the /var/log/syslog file
Code: Select all
* 22 * * * /usr/bin/python3 /home/pi/Desktop/myprog.py >$HOME/myprog.log 2>&1Code: Select all
[13:06:28.101739323] [891171] [1;32m INFO [1;37mCamera [1;34mcamera_manager.cpp:330 [0mlibcamera v0.5.2+99-bfd68f78
[13:06:28.142915903] [891226] [1;32m INFO [1;37mIPAProxy [1;34mipa_proxy.cpp:180 [0mUsing tuning file /usr/share/libcamera/ipa/rpi/vc4/imx219.json
[13:06:28.156452125] [891226] [1;32m INFO [1;37mCamera [1;34mcamera_manager.cpp:220 [0mAdding camera '/base/soc/i2c0mux/i2c@1/imx219@10' for pipeline handler rpi/vc4
[13:06:28.156621812] [891226] [1;32m INFO [1;37mRPI [1;34mvc4.cpp:440 [0mRegistered camera /base/soc/i2c0mux/i2c@1/imx219@10 to Unicam device /dev/media3 and ISP device /dev/media0
[13:06:28.156708843] [891226] [1;32m INFO [1;37mRPI [1;34mpipeline_base.cpp:1107 [0mUsing configuration file '/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml'
qt.qpa.xcb: could not connect to display
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, xcb.
Aborted
It can't find the x display, try DISPLAY=:0 /path/to/programRDS wrote: ↑Fri Oct 17, 2025 11:15 am@thagrol
Thank you once again.
At least I have an error message now, as follows:
My limited knowledge suggests this has something to do with a Qt platform plug, xcb, that is not available to crontab but is either available when I run the program 'normally', or is not required for normal running.Code: Select all
[13:06:28.101739323] [891171] [1;32m INFO [1;37mCamera [1;34mcamera_manager.cpp:330 [0mlibcamera v0.5.2+99-bfd68f78 [13:06:28.142915903] [891226] [1;32m INFO [1;37mIPAProxy [1;34mipa_proxy.cpp:180 [0mUsing tuning file /usr/share/libcamera/ipa/rpi/vc4/imx219.json [13:06:28.156452125] [891226] [1;32m INFO [1;37mCamera [1;34mcamera_manager.cpp:220 [0mAdding camera '/base/soc/i2c0mux/i2c@1/imx219@10' for pipeline handler rpi/vc4 [13:06:28.156621812] [891226] [1;32m INFO [1;37mRPI [1;34mvc4.cpp:440 [0mRegistered camera /base/soc/i2c0mux/i2c@1/imx219@10 to Unicam device /dev/media3 and ISP device /dev/media0 [13:06:28.156708843] [891226] [1;32m INFO [1;37mRPI [1;34mpipeline_base.cpp:1107 [0mUsing configuration file '/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml' qt.qpa.xcb: could not connect to display qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, xcb. Aborted
Using crontab (which I have been using for years) is a lot easier on older pi's with an older OS!
wcl55 wrote: ↑Fri Oct 17, 2025 11:29 amIt can't find the x display, try DISPLAY=:0 /path/to/programRDS wrote: ↑Fri Oct 17, 2025 11:15 am@thagrol
Thank you once again.
At least I have an error message now, as follows:
My limited knowledge suggests this has something to do with a Qt platform plug, xcb, that is not available to crontab but is either available when I run the program 'normally', or is not required for normal running.Code: Select all
[13:06:28.101739323] [891171] [1;32m INFO [1;37mCamera [1;34mcamera_manager.cpp:330 [0mlibcamera v0.5.2+99-bfd68f78 [13:06:28.142915903] [891226] [1;32m INFO [1;37mIPAProxy [1;34mipa_proxy.cpp:180 [0mUsing tuning file /usr/share/libcamera/ipa/rpi/vc4/imx219.json [13:06:28.156452125] [891226] [1;32m INFO [1;37mCamera [1;34mcamera_manager.cpp:220 [0mAdding camera '/base/soc/i2c0mux/i2c@1/imx219@10' for pipeline handler rpi/vc4 [13:06:28.156621812] [891226] [1;32m INFO [1;37mRPI [1;34mvc4.cpp:440 [0mRegistered camera /base/soc/i2c0mux/i2c@1/imx219@10 to Unicam device /dev/media3 and ISP device /dev/media0 [13:06:28.156708843] [891226] [1;32m INFO [1;37mRPI [1;34mpipeline_base.cpp:1107 [0mUsing configuration file '/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml' qt.qpa.xcb: could not connect to display qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, xcb. Aborted
Using crontab (which I have been using for years) is a lot easier on older pi's with an older OS!
RDS wrote: ↑Fri Oct 17, 2025 11:15 am@thagrol
Thank you once again.
At least I have an error message now, as follows:
My limited knowledge suggests this has something to do with a Qt platform plug, xcb, that is not available to crontab but is either available when I run the program 'normally', or is not required for normal running.Code: Select all
[13:06:28.101739323] [891171] [1;32m INFO [1;37mCamera [1;34mcamera_manager.cpp:330 [0mlibcamera v0.5.2+99-bfd68f78 [13:06:28.142915903] [891226] [1;32m INFO [1;37mIPAProxy [1;34mipa_proxy.cpp:180 [0mUsing tuning file /usr/share/libcamera/ipa/rpi/vc4/imx219.json [13:06:28.156452125] [891226] [1;32m INFO [1;37mCamera [1;34mcamera_manager.cpp:220 [0mAdding camera '/base/soc/i2c0mux/i2c@1/imx219@10' for pipeline handler rpi/vc4 [13:06:28.156621812] [891226] [1;32m INFO [1;37mRPI [1;34mvc4.cpp:440 [0mRegistered camera /base/soc/i2c0mux/i2c@1/imx219@10 to Unicam device /dev/media3 and ISP device /dev/media0 [13:06:28.156708843] [891226] [1;32m INFO [1;37mRPI [1;34mpipeline_base.cpp:1107 [0mUsing configuration file '/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml' qt.qpa.xcb: could not connect to display qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, xcb. Aborted
Using crontab (which I have been using for years) is a lot easier on older pi's with an older OS!
Code: Select all
* 22 * * * DISPLAY=:0 /usr/bin/python3 /home/pi/Desktop/myprog.py >$HOME/myprog.log 2>&1If you are running from CRON, why do you need to display a preview? Simply capture the file.Code: Select all
picam2.start_preview(Preview.QT, x=200, y=200, width=400, height=300) picam2.start() # this line starts the preview
That's a very good point.DS256 wrote: ↑Fri Oct 17, 2025 12:50 pmIf you are running from CRON, why do you need to display a preview? Simply capture the file.Code: Select all
picam2.start_preview(Preview.QT, x=200, y=200, width=400, height=300) picam2.start() # this line starts the preview
RDS wrote: ↑Fri Oct 17, 2025 1:13 pmThat's a very good point.DS256 wrote: ↑Fri Oct 17, 2025 12:50 pmIf you are running from CRON, why do you need to display a preview? Simply capture the file.Code: Select all
picam2.start_preview(Preview.QT, x=200, y=200, width=400, height=300) picam2.start() # this line starts the preview