I have a python script to record a .mp4 file with picamera2 which executes and save correctly when run through the editor (thonny) and through the terminal. However, when I schedule it through crontab to run at boot (or through bash.rc) the script runs (I hear the camera click on) but no file is saved in either the home directory or the directory that I've called in the python script. Is there some piece that I'm missin in cron to get it to actually save?
#!/usr/bin/python3
import time
import os
from picamera2 import Picamera2
from picamera2.encoders import H264Encoder
from picamera2.outputs import FfmpegOutput
timestr = time.strftime("%Y%m%d-%H%M%S")
picam2 = Picamera2()
video_config = picam2.create_video_configuration()
picam2.configure(video_config)
os.chdir('/media/pi/USB/VIDEO')
encoder = H264Encoder(10000000)
output = FfmpegOutput(timestr+'.mp4', audio=True, )
picam2.start_recording(encoder, output)
time.sleep(10)
picam2.stop_recording()
I've also tried getting putting this into a shell script and scheduling the shell to run in cron. Same issue - the shell will run the python script successfully and save a file appropriately when executed from terminal, but not when scheduled:
cd /
cd home/pi/
/usr/bin/python3 /home/pi/VideoPythonTest.py
cd /
Cron call:
@reboot sh /home/pi/launcher.sh
env report follows for user LANGUAGE=en_US.UTF-8 HOME=/root LOGNAME=root PATH=/usr/bin:/bin LANG=en_US.UTF-8 SHELL=/bin/sh LC_ALL=en_US.UTF-8 PWD=/root env report for user concluded
Having HOME = /root seems like it could be the probelm?
Could you elaborate on what you mean by "PATH environment in your launcher.sh to be the same as the interactive shell?"
– roconnor Dec 12 '22 at 17:22PATH=/usr/bin:/bin
in your comment. Do a bit of research on the PATH environment variable to understand its role. Briefly: Your system must know where to find any executable you call. Since it works from your interactive shell (which has a different PATH thancron
, one solution is to add its PATH to your shell script. When I get back from dinner, I'll add an edit to my answer with a change to your shell script. that may work. – Seamus Dec 13 '22 at 01:00