You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派相机crontab定时任务异常:脚本手动可执行但定时失败求助

Troubleshooting Crontab Execution for Raspberry Pi Timelapse Camera Scripts

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

  1. watch isn't built for non-interactive environments: Crontab runs tasks in a headless, terminal-less context, but watch is designed for interactive terminal sessions. Using it here can cause silent failures because it expects a terminal output that doesn't exist.
  2. Duplicate script instances: Your watch -n 10 command spawns a new process1.sh every 10 seconds, leading to dozens of overlapping processes trying to write to the same imageBTW.jpg file. This causes conflicts, file corruption, and unexpected behavior—even if manual runs work when only one instance is active.
  3. Missing absolute paths for commands: Crontab uses a stripped-down PATH environment variable. Commands like raspistill or convert might not be found unless you specify their full system paths.
  4. Redundant crontab entries: You don't need to repeat tasks for each weekday—1-5 covers 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 22:07:28