Cron任务无法执行求助:手动可运行的定时脚本失效
Alright, let's dig into why your cron job might not be running even though the script works perfectly when you run it manually. I've troubleshooted this exact scenario countless times, so let's break down the most common fixes step by step.
1. Fix Environment Variable Differences (The #1 Culprit)
Cron runs in a stripped-down environment—its PATH is way shorter than your regular user shell, and it doesn't load your .bashrc or .profile by default. That means:
- Always use absolute paths for commands (like
ping) and files in your script.- Find
ping's absolute path withwhich ping(usually/bin/pingor/usr/bin/ping). - Use full paths for log files instead of relative ones (e.g.,
/home/youruser/ping_logs/google_ping.loginstead ofping_logs/google_ping.log).
- Find
Example fixed script snippet:
#!/bin/bash LOG_PATH="/home/youruser/ping_results.log" /bin/ping -c 4 google.com >> $LOG_PATH 2>&1
2. Log Cron's Execution Output
Cron doesn't show you errors by default—you have to capture them. Either:
- Check system cron logs (location varies by distro:
/var/log/cron,/var/log/syslog, or/var/log/messages). Runtail -f /var/log/cronto watch real-time activity. - Or, redirect all output from your cron job to a debug log:
This logs both standard output and errors, so you'll see exactly what's failing.* * * * * /full/path/to/your/script.sh >> /home/youruser/cron_debug.log 2>&1
3. Verify Script Shebang & Permissions
Double-check two key things:
- The script's shebang uses an absolute path for bash:
#!/bin/bash(not#!/bash—cron might not find bash otherwise). - Ensure the script is executable:
chmod +x /full/path/to/your/script.sh - Also confirm you have write permissions for the log directory (create it first if needed:
mkdir -p /home/youruser/ping_logs).
4. Validate Cron Syntax & Task
Run crontab -l to list your current cron jobs and make sure:
- The path to your script is absolute (no relative paths here either).
- The timing syntax is correct:
- Test mode (every minute):
* * * * * /full/path/to/script.sh - Production mode (every 10 minutes):
*/10 * * * * /full/path/to/script.sh
(Remember: cron's order is minute, hour, day, month, week—don't mix these up!)
- Test mode (every minute):
5. Simulate Cron's Environment Manually
To replicate exactly how cron runs your script, use this command (replace paths with your own):
env -i PATH=/usr/bin:/bin HOME=/home/youruser /full/path/to/your/script.sh
If this fails, you'll know the issue is with missing environment variables, not the script itself.
6. Check if Cron Service is Running
It sounds obvious, but sometimes cron can stop unexpectedly. Verify it's active:
# Systemd systems (Ubuntu 16.04+, Debian 9+, RHEL 7+) systemctl status cron # Older sysvinit systems service cron status
If it's not running, start it with systemctl start cron or service cron start.
Example Working Setup
Here's a complete example to reference:
Script (/home/jermayne/ping_google.sh)
#!/bin/bash LOG_DIR="/home/jermayne/logs" LOG_FILE="$LOG_DIR/google_ping.log" # Create log directory if it doesn't exist mkdir -p $LOG_DIR # Log timestamp and ping results echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting ping test..." >> $LOG_FILE /bin/ping -c 4 google.com >> $LOG_FILE 2>&1 echo "[$(date '+%Y-%m-%d %H:%M:%S')] Ping test finished" >> $LOG_FILE echo "----------------------------------------" >> $LOG_FILE
Set Permissions
chmod +x /home/jermayne/ping_google.sh
Cron Task (Add via crontab -e)
# Test: Run every minute * * * * * /home/jermayne/ping_google.sh # Production: Run every 10 minutes (uncomment later) # */10 * * * * /home/jermayne/ping_google.sh
内容的提问来源于stack exchange,提问作者Jermayne Williams




