We use some essential cookies to make our website work.

We use optional cookies, as detailed in our cookie policy, to remember your settings and understand how you use our website.

RDS
Posts: 914
Joined: Tue Oct 06, 2015 8:17 am

How do I get crontab to run a Python program

Tue Oct 14, 2025 8:41 pm

I have used crontab successfully many times on earlier versions of Raspberry Pi OS (for example Pi Zero W Rev 1.1, running jessie).
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.
I seem to recall reading that it could be something to do with path and environment variables (whatever that means)

Previously I could have a python 3 program stored on the desktop and just add a few commands to a crontab job opened using crontab -e and it would run.

Any help would be very much appreciated to run a crontab job on the Model 3B+ running bookworm.

ame
Posts: 11565
Joined: Sat Aug 18, 2012 1:21 am

DS256
Posts: 1288
Joined: Mon Jul 26, 2021 7:44 pm

Re: How do I get crontab to run a Python program

Tue Oct 14, 2025 9:00 pm

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.
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.

If running in a VENV then you need to specify the Python executable in the VENV. For example, for the VENV defined under ~/.venv/wc2 the syntax is /home/pi/.venv/wc2/bin/python3

kerry_s
Posts: 8464
Joined: Thu Jan 30, 2020 7:14 pm

Re: How do I get crontab to run a Python program

Tue Oct 14, 2025 9:06 pm

for 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

thagrol
Posts: 14784
Joined: Fri Jan 13, 2012 4:41 pm

Re: How do I get crontab to run a Python program

Tue Oct 14, 2025 11:38 pm

kerry_s wrote:
Tue Oct 14, 2025 9:06 pm
for 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

Really?

For something that requires the desktop without a venv maybe. If the desktop is running and that user is logged in to it. Or if the security settings have been relaxed on whatever desktop is running.

But even then I'm not sure if it would actually work. Under cron $DISPLAY is not set so export DISPLAY="$(echo $DISPLAY)" will do no more than create an empty environment variable called DISPLAY. I also don't see the advantage of using export DISPLAY="$(echo $DISPLAY)" over a simple export DISPLAY. Both achieve the same result whetehr $DISPLAY is set or not.

I've run plenty of non desktop, non interactive python scripts from cron without any of the above.
Knowledge, skills, & experience have value. If you expect to profit from someone's you should expect to pay for them.

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

kerry_s
Posts: 8464
Joined: Thu Jan 30, 2020 7:14 pm

Re: How do I get crontab to run a Python program

Wed Oct 15, 2025 1:15 am

oh the chaos

it's like all things, depends what your running, may need it, may not

is it really going to kill you to check, what if he's not user 1000 or his display is not 0
Attachments
Screenshot From 2025年10月14日 15-10-59.png
Screenshot From 2025年10月14日 15-10-59.png (22.43 KiB) Viewed 1356 times

RDS
Posts: 914
Joined: Tue Oct 06, 2015 8:17 am

Re: How do I get crontab to run a Python program

Wed Oct 15, 2025 12:58 pm

I am grateful for the responses but they don't appear to answer my question. I don't actually know what Virtual Environment (VENV) or stand-alone means.
If I could try again to ask my question with more detail.
This is my program stored on my Raspberry Pi Model 3B+

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')
I want to run it using cron and I am using the following line in a crontab job, which I get into using crontab -e.
My line of code in cron is:
* 22 * * * python3 /home/pi/Desktop/myprog.py

The Camera is mounted in my Solar Panel Meter box to take a picture of the meter at 10pm each night and store it on the Desktop and in the Pictures folder.
The program runs perfectly from either Thonny or Geany

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.

thagrol
Posts: 14784
Joined: Fri Jan 13, 2012 4:41 pm

Re: How do I get crontab to run a Python program

Wed Oct 15, 2025 1:03 pm

Usual troubleshooting steps apply:
  1. Find out why it's failing.
  2. Fix it
  3. ...
  4. Profit
See section 6.1 of my guide that ame linked to earlier.

Until you perform step 1 all we can do is guess.
Knowledge, skills, & experience have value. If you expect to profit from someone's you should expect to pay for them.

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

DS256
Posts: 1288
Joined: Mon Jul 26, 2021 7:44 pm

Re: How do I get crontab to run a Python program

Wed Oct 15, 2025 1:06 pm

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.
Can you run the program manually, outside of CRON successfully e.g.

Code: Select all

$ python3 /home/pi/Desktop/myprog.py

RDS
Posts: 914
Joined: Tue Oct 06, 2015 8:17 am

Re: How do I get crontab to run a Python program

Wed Oct 15, 2025 1:20 pm

DS256 wrote:
Wed Oct 15, 2025 1:06 pm
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.
Can you run the program manually, outside of CRON successfully e.g.

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.
(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
Posts: 14784
Joined: Fri Jan 13, 2012 4:41 pm

Re: How do I get crontab to run a Python program

Wed Oct 15, 2025 2:24 pm

RDS wrote:
Wed Oct 15, 2025 1:20 pm
DS256 wrote:
Wed Oct 15, 2025 1:06 pm
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.
Can you run the program manually, outside of CRON successfully e.g.

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.
(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.
Knowledge, skills, & experience have value. If you expect to profit from someone's you should expect to pay for them.

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

RDS
Posts: 914
Joined: Tue Oct 06, 2015 8:17 am

Re: How do I get crontab to run a Python program

Wed Oct 15, 2025 8:36 pm

thagrol wrote:
Wed Oct 15, 2025 2:24 pm
RDS wrote:
Wed Oct 15, 2025 1:20 pm
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
Yes, I can, it works as expected. Thanks, I was not aware you could run a program like that.
(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.

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?

I can assure you I have not been waiting until 22:00 and I have been adding a couple of minutes to the current time each time I have tried it.

DS256
Posts: 1288
Joined: Mon Jul 26, 2021 7:44 pm

Re: How do I get crontab to run a Python program

Wed Oct 15, 2025 8:56 pm

In section 6.1, I ran the second error finding option (grep .... ) and this resulted in a 'No such file or directory' error
Please post your CRONTAB definitions

Code: Select all

crontab -l
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?
That is an example based on the examples shown previously. You are suppose to add the 2> to your command.

RDS
Posts: 914
Joined: Tue Oct 06, 2015 8:17 am

Re: How do I get crontab to run a Python program

Wed Oct 15, 2025 10:22 pm

Thank you. I will continue with this tomorrow, it's getting late

thagrol
Posts: 14784
Joined: Fri Jan 13, 2012 4:41 pm

Re: How do I get crontab to run a Python program

Wed Oct 15, 2025 10:45 pm

RDS wrote:
Wed Oct 15, 2025 8:36 pm
thagrol wrote:
Wed Oct 15, 2025 2:24 pm
RDS wrote:
Wed Oct 15, 2025 1:20 pm


Yes, I can, it works as expected. Thanks, I was not aware you could run a program like that.
(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.

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"
Knowledge, skills, & experience have value. If you expect to profit from someone's you should expect to pay for them.

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

RDS
Posts: 914
Joined: Tue Oct 06, 2015 8:17 am

Re: How do I get crontab to run a Python program

Thu Oct 16, 2025 9:44 pm

thagrol wrote:
Wed Oct 15, 2025 10:45 pm
RDS wrote:
Wed Oct 15, 2025 8:36 pm
thagrol 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"
Thank 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

thagrol
Posts: 14784
Joined: Fri Jan 13, 2012 4:41 pm

Re: How do I get crontab to run a Python program

Thu Oct 16, 2025 10:13 pm

RDS wrote:
Thu Oct 16, 2025 9:44 pm
Thank 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

It would. It's an example cron tab entry not an example command to run at the prompt.
Further questions/points if I may
1) Does the pi need to be rebooted after any change to the crontab file?

Only if you have an @reboot cronjob that you want to trigger. Timed jobs should run at the next specified time.
2) How do I know which crontab file to use (e.g. sudo crontab -e or just crontab -e or is there another one)

Generally you don't use sudo crontab -e unless you want to run the process as root (think admin if used to Windows) or unless the process needs to be run with those privileges.

It's the same guideline as for sudo: don't use it unless you absolutely have to.

There are other places. See sections 7.3, 7.4, and 7.5 Best advice is to pick one and stick to it for all cron jobs. My preference being crontab -e
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

I believe I explained that in my last post. At least for RPiOS Trixie. It may also apply to RPiOS Bookworm. I'm not sure exactly when that change came in.

You're obviously struggling with this so I'm going to do something I wouldn't normally* do and suggest a modified crontab entry for you to try:

Code: Select all

* 22 * * * /usr/bin/python3 /home/pi/Desktop/myprog.py >$HOME/myprog.log 2>&1
That specifies the full path to your system python3 binary and sends all output (including any error messages) to myprog.log in the user's home directory. If you use root's crontab (sudo contab -e) that's /root.

*: Because you'll learn more by doing than you will by copy'n'paste.
Knowledge, skills, & experience have value. If you expect to profit from someone's you should expect to pay for them.

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

RDS
Posts: 914
Joined: Tue Oct 06, 2015 8:17 am

Re: How do I get crontab to run a Python program

Fri Oct 17, 2025 11:15 am

@thagrol
Thank you once again.

At least I have an error message now, as follows:

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
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.
Using crontab (which I have been using for years) is a lot easier on older pi's with an older OS!

wcl55
Posts: 175
Joined: Fri Jun 13, 2025 3:14 pm

Re: How do I get crontab to run a Python program

Fri Oct 17, 2025 11:29 am

RDS wrote:
Fri Oct 17, 2025 11:15 am
@thagrol
Thank you once again.

At least I have an error message now, as follows:

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
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.
Using crontab (which I have been using for years) is a lot easier on older pi's with an older OS!
It can't find the x display, try DISPLAY=:0 /path/to/program

thagrol
Posts: 14784
Joined: Fri Jan 13, 2012 4:41 pm

Re: How do I get crontab to run a Python program

Fri Oct 17, 2025 11:48 am

wcl55 wrote:
Fri Oct 17, 2025 11:29 am
RDS wrote:
Fri Oct 17, 2025 11:15 am
@thagrol
Thank you once again.

At least I have an error message now, as follows:

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
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.
Using crontab (which I have been using for years) is a lot easier on older pi's with an older OS!
It can't find the x display, try DISPLAY=:0 /path/to/program

There is no X display. There's only wayland/labwc running an X11 compatability shim.
Knowledge, skills, & experience have value. If you expect to profit from someone's you should expect to pay for them.

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

thagrol
Posts: 14784
Joined: Fri Jan 13, 2012 4:41 pm

Re: How do I get crontab to run a Python program

Fri Oct 17, 2025 11:54 am

RDS wrote:
Fri Oct 17, 2025 11:15 am
@thagrol
Thank you once again.

At least I have an error message now, as follows:

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
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.
Using crontab (which I have been using for years) is a lot easier on older pi's with an older OS!

Looks like something in your code (or something it relies on) needs the Desktop.

Try

Code: Select all

* 22 * * * DISPLAY=:0 /usr/bin/python3 /home/pi/Desktop/myprog.py >$HOME/myprog.log 2>&1
You may need to use the same user with cron as is logged in to the desktop.

[edit]
It works from thonny, geany, and a terminal window because the desktop is running and the required environment variable (DISPLAY) is set for you. Under cron DISPLAY is not set so even if the desktop is running your process doesn't know where to find it.
[/edit]
Knowledge, skills, & experience have value. If you expect to profit from someone's you should expect to pay for them.

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

DS256
Posts: 1288
Joined: Mon Jul 26, 2021 7:44 pm

Re: How do I get crontab to run a Python program

Fri Oct 17, 2025 12:50 pm

Code: Select all

picam2.start_preview(Preview.QT, x=200, y=200, width=400, height=300)
picam2.start() # this line starts the preview
If you are running from CRON, why do you need to display a preview? Simply capture the file.

RDS
Posts: 914
Joined: Tue Oct 06, 2015 8:17 am

Re: How do I get crontab to run a Python program

Fri Oct 17, 2025 1:12 pm

@thagrol
It worked. Excellent and thank you for your continuous support over the last few days. Your error trapping routine enabled the problem to be found. Incidentally, I am the only user and all my Raspberry Pi's are run headless and viewed via Real VNC.

and thank you to wcl55, it needed the display=:0 addition to the cron line
Last edited by RDS on Fri Oct 17, 2025 1:15 pm, edited 1 time in total.

RDS
Posts: 914
Joined: Tue Oct 06, 2015 8:17 am

Re: How do I get crontab to run a Python program

Fri Oct 17, 2025 1:13 pm

DS256 wrote:
Fri Oct 17, 2025 12:50 pm

Code: Select all

picam2.start_preview(Preview.QT, x=200, y=200, width=400, height=300)
picam2.start() # this line starts the preview
If you are running from CRON, why do you need to display a preview? Simply capture the file.
That's a very good point.

thagrol
Posts: 14784
Joined: Fri Jan 13, 2012 4:41 pm

Re: How do I get crontab to run a Python program

Fri Oct 17, 2025 2:12 pm

RDS wrote:
Fri Oct 17, 2025 1:13 pm
DS256 wrote:
Fri Oct 17, 2025 12:50 pm

Code: Select all

picam2.start_preview(Preview.QT, x=200, y=200, width=400, height=300)
picam2.start() # this line starts the preview
If you are running from CRON, why do you need to display a preview? Simply capture the file.
That's a very good point.

It's worth trying without the preview and without setting DISPLAY. Or if you do need the preview see if it's possible to do it the old way where it writes directly to the frame buffer bypassing the desktop entirely. I can't say whether it is as I've done nothing with the new(ish) camera stack.
Knowledge, skills, & experience have value. If you expect to profit from someone's you should expect to pay for them.

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

Return to "Python"

AltStyle によって変換されたページ (->オリジナル) /