DEV Community

Sospeter Mong'are
Sospeter Mong'are

Posted on

1

How to Check Gunicorn Logs and Monitor Your Django App as a Systemd Service

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
Enter fullscreen mode Exit fullscreen mode
  • -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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Create the Logs Directory

sudo mkdir -p /var/www/tunaresq_be/logs
sudo chown -R $USER:$USER /var/www/tunaresq_be/logs
Enter fullscreen mode Exit fullscreen mode

Then reload and restart:

sudo systemctl daemon-reload
sudo systemctl restart tunaresq_be
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Check Socket File

ls -la /var/www/tunaresq_be/tunaresq_be.sock
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Meet the fully integrated developer platform.

Meet the fully integrated developer platform.

Modern auth, access management and billing for engineers.

Get a free account

Top comments (0)

DevCycle image

Ship Faster, Stay Flexible.

DevCycle is the first feature flag platform with OpenFeature built-in to every open source SDK, designed to help developers ship faster while avoiding vendor-lock in.

Start shipping

Announcing the First DEV Education Track: "Build Apps with Google AI Studio"

The moment is here! We recently announced DEV Education Tracks, our new initiative to bring you structured learning paths directly from industry experts.

Dive in and Learn

DEV is bringing Education Tracks to the community. Dismiss if you're not interested. ❤️