Airflow问题:包含单个任务的简单DAG始终无法完成
Hey there, let's figure out why your log cleanup task won't finish up. I've tackled similar Airflow headaches before, so here are some practical steps to diagnose the problem:
1. Test the Script Manually First
The first thing to rule out is whether the script itself works when you run it directly. Open a terminal and execute:
/home/ubuntu/airflow/dags/scripts/log_cleanup/log_cleanup.sh
- If it hangs here, the issue is with the script, not Airflow. Look for infinite loops, prompts waiting for user input, or commands that take way longer than expected to run.
- If it throws an error (like "permission denied" or "file not found"), that's your first concrete clue.
2. Dig Into the Task's Airflow Logs
Airflow's UI has detailed logs for every task run—this is gold for troubleshooting. Navigate to your log_cleanup DAG, click into the stuck cleanup_task, and check the logs for:
- Path typos: Even a tiny mistake (like a missing directory or extra space) will break the script execution.
- Permission blocks: Does the Airflow user have access to the script or the log directory it's supposed to clean?
- Script-specific errors: Any output from the script itself that shows it failed partway through.
3. Tweak the BashOperator Configuration
A couple of small adjustments might fix the hang:
- You have an extra space at the end of your
cleanup_commandstring (...log_cleanup.sh). While this usually isn't critical, it's worth removing to eliminate any potential parsing weirdness:cleanup_command = "/home/ubuntu/airflow/dags/scripts/log_cleanup/log_cleanup.sh" - Add an execution timeout to prevent the task from hanging indefinitely. If your script should finish in, say, 10 minutes, update your BashOperator:
If the task times out, you'll know the script is taking longer than expected.from datetime import timedelta t1 = BashOperator( task_id='cleanup_task', bash_command=cleanup_command, execution_timeout=timedelta(minutes=10), dag=dag )
4. Check Airflow Worker Health
If the worker running your task is unresponsive, the task will stay stuck in a "running" state:
- If you're using systemd to manage Airflow, run
systemctl status airflow-workerto confirm the service is active and healthy. - For manual worker setups, use
airflow worker status(adjust based on your Airflow version) to make sure the worker is running and processing tasks. - Also, confirm the worker's environment matches your local terminal—scripts sometimes rely on environment variables that aren't set for the Airflow user.
5. Verify Permissions
Airflow runs under a specific user (often airflow or the user who started the service). Make sure this user has:
- Execute permissions on the
log_cleanup.shscript: Runchmod +x /home/ubuntu/airflow/dags/scripts/log_cleanup/log_cleanup.shif needed. - Read/write/delete access to the log directory the script targets. Test this by running:
If this fails, adjust directory permissions withsudo -u airflow rm /path/to/old/log/file.logchmodorchown.
Start with the first step (testing the script manually)—it's the fastest way to narrow down the problem. Let me know what you find!
内容的提问来源于stack exchange,提问作者Wessi

