systemd任务启动失败提示“无此文件或目录”,Airflow自启动脚本求助
Let's walk through why your systemd service is throwing that "No such file or Directory" error and fix it step by step—this is a common pitfall when setting up custom startup scripts with systemd.
1. Fix Path Issues in Your Script
Systemd runs in a minimal environment, so shortcuts like ~ won’t resolve to your user’s home directory, and it might not have the same PATH as your regular shell session. Here’s what to adjust:
- Replace
~/airflowwith the absolute path to your Airflow home (e.g.,/home/ubuntu/airflow). - Use the full path for the
airflowcommand (runwhich airflowto find it—likely/usr/local/bin/airflow). - Double-check that
/home/ubuntu/Source/Bash/airflow-start-webserver.shexists and uses absolute paths internally too. - Remove
gnome-terminalentirely: Systemd doesn’t have access to a graphical session at boot, so launching a terminal will fail. Instead, run your webserver script directly in the background (add&at the end).
Here’s a revised version of your script with these fixes:
#!/bin/bash set -x # Enable debug output to track exactly where things fail echo "Starting postgres service" export AIRFLOW_HOME=/home/ubuntu/airflow sudo service postgresql start echo "Sleeping to let postgres initialize" sleep 15 echo "Initializing Airflow database" /usr/local/bin/airflow initdb echo "Sleeping to let database setup complete" sleep 15 # Run webserver script in the background instead of using gnome-terminal /home/ubuntu/Source/Bash/airflow-start-webserver.sh &
2. Verify Script Permissions and Ownership
Make sure your script is executable and accessible to the user running the systemd service:
- Add execute permissions:
chmod +x /usr/local/bin/airflow-start-all.sh - If your systemd service runs as a specific user (e.g.,
ubuntu), ensure the script is owned by that user or has world-readable/execute permissions:chown ubuntu:ubuntu /usr/local/bin/airflow-start-all.sh
3. Fix Your Systemd Service File
Most of the time, the error comes from a misconfigured service file. Here’s a working example (save as /etc/systemd/system/airflow.service):
[Unit] Description=Airflow Startup Service After=postgresql.target network.target # Wait for postgres and network to be ready [Service] User=ubuntu Group=ubuntu ExecStart=/usr/local/bin/airflow-start-all.sh # Use the FULL path to your script WorkingDirectory=/home/ubuntu # Set working directory to your user's home Restart=on-failure # Restart if the service crashes [Install] WantedBy=multi-user.target
4. Debug with Systemd Logs
To pinpoint exactly which file/directory is missing, check the systemd logs in real-time:
sudo journalctl -u airflow.service -f
This will show you every step the service tries to run, so you can see which command triggers the "No such file or Directory" error.
Final Steps to Apply Changes
After making adjustments:
- Reload systemd to pick up the new service file:
sudo systemctl daemon-reload - Start the service and check its status:
sudo systemctl start airflow.service sudo systemctl status airflow.service - If you want it to start on boot:
sudo systemctl enable airflow.service
内容的提问来源于stack exchange,提问作者mitroberts

