树莓派相机crontab定时任务异常:脚本手动可执行但定时失败求助
Let's break down why your crontab setup isn't working, and fix it step by step—plus optimize your workflow to avoid future headaches.
First, the Core Issues in Your Current Setup
watchisn't built for non-interactive environments: Crontab runs tasks in a headless, terminal-less context, butwatchis designed for interactive terminal sessions. Using it here can cause silent failures because it expects a terminal output that doesn't exist.- Duplicate script instances: Your
watch -n 10command spawns a newprocess1.shevery 10 seconds, leading to dozens of overlapping processes trying to write to the sameimageBTW.jpgfile. This causes conflicts, file corruption, and unexpected behavior—even if manual runs work when only one instance is active. - Missing absolute paths for commands: Crontab uses a stripped-down
PATHenvironment variable. Commands likeraspistillorconvertmight not be found unless you specify their full system paths. - Redundant crontab entries: You don't need to repeat tasks for each weekday—
1-5covers Monday to Friday.
Step 1: Fix the process1.sh Script
Rewrite your script to handle the 10-second loop internally, check for running instances, and use absolute command paths. Here's the improved version:
#!/bin/bash # Set full PATH to avoid command-not-found errors PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games # Prevent duplicate runs (check if another process1.sh is already active) if pgrep -f "process1.sh" | grep -v $$ > /dev/null; then echo "Another process1.sh instance is running—exiting." exit 0 fi # Define end time (5 PM) to stop the loop automatically END_TIMESTAMP=$(date -d "17:00" +%s) while [ $(date +%s) -lt $END_TIMESTAMP ]; do # Take photo with absolute path to raspistill /usr/bin/raspistill -w 1920 -h 1080 -o /home/pi/timelapse/images/imageBTW.jpg # Generate timestamp for the final image DATE=$(date +"%Y-%m-%d_%H%M%S") # Add watermark with absolute path to convert /usr/bin/convert "/home/pi/timelapse/images/imageBTW.jpg" \ -pointsize 72 -fill white -annotate +100+100 \ %[exif:DateTimeOriginal] "/home/pi/timelapse/newimage/$DATE.jpg" # Clean up temporary file rm /home/pi/timelapse/images/imageBTW.jpg # Wait 10 seconds before next shot sleep 10 done
Step 2: Set Script Permissions
Make sure your scripts are executable:
chmod +x /home/pi/timelapse/process1.sh chmod +x /home/pi/timelapse/newimage/video.sh
Step 3: Simplify Your Crontab
Replace all your existing crontab entries with these concise, reliable lines (run crontab -e to edit):
# Start timelapse script at 9 AM every weekday (Mon-Fri) 0 9 * * 1-5 /bin/bash /home/pi/timelapse/process1.sh # Run video synthesis at 5:30 PM every weekday (Mon-Fri) 30 17 * * 1-5 /bin/bash /home/pi/timelapse/newimage/video.sh
Note: We removed the sudo reboot task because the script now stops automatically at 5 PM—no need to restart the system to terminate it.
Step 4: Debug with Crontab Logs
If you still run into issues, check the cron service logs to see exactly what's failing:
journalctl -u cron.service
Look for error messages like "command not found" (which confirms path issues) or permission denials.
Bonus: Verify Command Paths
If you're unsure about the absolute path of raspistill or convert, run these commands to get their locations:
which raspistill # Typically outputs /usr/bin/raspistill which convert # Typically outputs /usr/bin/convert
Update the paths in process1.sh if they don't match.
内容的提问来源于stack exchange,提问作者Alejandro Sorní Soler

