When deploying a Django application using Gunicorn and systemd, checking logs and ensuring everything is running smoothly is essential. Whether you're troubleshooting errors or simply validating a successful deployment, this guide walks you through how to manage and monitor your Gunicorn process on a Linux server.
1. Check Gunicorn Logs Using journalctl
If your Gunicorn is running under a systemd service (e.g., tunaresq_be.service
), the easiest way to view logs is with journalctl
.
View Recent Logs
sudo journalctl -u tunaresq_be.service -e
-
-u tunaresq_be.service
: Filters logs for your service. -
-e
: Jumps to the latest entries.
Follow Logs in Real Time
sudo journalctl -u tunaresq_be.service -f
This works like tail -f
and is useful for watching logs live while restarting services or testing requests.
2. Check Gunicorn Service Status
You can get a summary of the service status and any recent failures using:
sudo systemctl status tunaresq_be
This will show:
- Whether the service is active
- The main PID
- Recent error or info messages
3. Enable Gunicorn File Logging (Optional)
If you want to keep dedicated access and error logs (besides what systemd
collects), you can modify your tunaresq_be.service
file to include log files.
Example Service File: /etc/systemd/system/tunaresq_be.service
[Unit]
Description=Gunicorn daemon for tunaresq-be
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/tunaresq_be
ExecStart=/var/www/tunaresq_be/venv/bin/gunicorn \
--access-logfile /var/www/tunaresq_be/logs/access.log \
--error-logfile /var/www/tunaresq_be/logs/error.log \
--workers 3 \
--bind unix:/var/www/tunaresq_be/tunaresq_be.sock \
tunaresq_be.wsgi:application
[Install]
WantedBy=multi-user.target
Create the Logs Directory
sudo mkdir -p /var/www/tunaresq_be/logs
sudo chown -R $USER:$USER /var/www/tunaresq_be/logs
Then reload and restart:
sudo systemctl daemon-reload
sudo systemctl restart tunaresq_be
Now, your logs will be saved to:
/var/www/tunaresq_be/logs/access.log
/var/www/tunaresq_be/logs/error.log
4. Confirm Gunicorn is Listening on the Socket
If your Gunicorn binds to a UNIX socket, ensure it's actually running and the socket exists:
List Gunicorn Sockets
sudo lsof -nP -U | grep gunicorn
Check Socket File
ls -la /var/www/tunaresq_be/tunaresq_be.sock
If the socket file doesn’t exist, Gunicorn likely failed to start—check logs via journalctl
or your error.log
.
🛠️ Quick Commands Summary
# View Gunicorn logs
sudo journalctl -u tunaresq_be.service -e
# Follow logs live
sudo journalctl -u tunaresq_be.service -f
# Restart and reload service
sudo systemctl daemon-reload
sudo systemctl restart tunaresq_be
# Check service status
sudo systemctl status tunaresq_be
Final Tip
Always ensure your Gunicorn service:
- Has correct permissions for socket and logs
- Logs meaningful errors (via
--error-logfile
) - Is managed cleanly using
systemctl