DEV Community

Chetan Singh
Chetan Singh

Posted on

How to Manage Background Services with systemctl and systemd (with Celery Example)

When deploying services like Celery, Gunicorn, Redis, etc., on a Linux server — you don’t want to manually start them every time your server reboots. That’s where systemd and systemctl come in.

In this guide, I’ll explain how to:

  • Understand what systemctl is
  • Create a systemd service for something like Celery
  • Use essential commands to manage any service
  • Troubleshoot and view logs

What is systemd and systemctl?

  • systemd is the init system and service manager used by most modern Linux distributions.
  • systemctl is the command-line tool to interact with systemd.

It's used to start, stop, restart, enable (auto-start on boot), and debug services.

When Do You Need a systemd Service?
If you're running long-lived processes like:

  • Celery worker
  • Gunicorn for a Django/Flask app
  • Background scripts
  • Queues / job runners
  • Redis / PostgreSQL / any server

And you're not using Docker or Kubernetes, it’s best to define a systemd unit file for each of them.

Where Are These Service Files Stored?

| Location                  | Purpose                               |
| ------------------------- | ------------------------------------- |
| `/etc/systemd/system/`    | 🔧 Your custom services (recommended) |
| `/lib/systemd/system/`    | 📦 Package-installed services         |
| `~/.config/systemd/user/` | 👤 Per-user services (less common)    |
Enter fullscreen mode Exit fullscreen mode

Use /etc/systemd/system/ for your custom services like Celery.

🧰 Example: Create a Celery Systemd Service

Assume:

  • Your app is in /home/ubuntu/yourproject
  • You’re using a virtual environment at /home/ubuntu/venv
  • Your Celery app is named yourproject Create the service file:
sudo nano /etc/systemd/system/celery.service

Enter fullscreen mode Exit fullscreen mode
Paste this:
[Unit]
Description=Celery Worker Service
After=network.target

[Service]
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/yourproject
ExecStart=/home/ubuntu/venv/bin/celery -A yourproject worker --loglevel=info
Restart=always

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Enable and start the service

# Reload systemd to recognize the new service
sudo systemctl daemon-reload

# Enable to start on boot
sudo systemctl enable celery

# Start it now
sudo systemctl start celery
Enter fullscreen mode Exit fullscreen mode

🧾 Handy systemctl Commands

Command Description
sudo systemctl start celery Start service
sudo systemctl stop celery Stop service
sudo systemctl restart celery Restart it
sudo systemctl status celery View status
sudo systemctl enable celery Start on boot
sudo systemctl disable celery Remove from startup
sudo systemctl daemon-reload Reload configs (after editing .service)

📑 View Logs with journalctl

# View logs for celery service
sudo journalctl -u celery

# Follow logs in real-time (like tail -f)
sudo journalctl -u celery -f
Enter fullscreen mode Exit fullscreen mode

🧹 Bonus: Reload After Edits

Any time you edit a .service file, run:
sudo systemctl daemon-reload

💭 Conclusion

Using systemd with systemctl helps you make your production deployments clean, auto-recovering, and reboot-friendly.

Instead of manually managing your services, just define them once and let Linux take care of the rest.

_Written using Chatgpt for quick one stop basic guide. _

Top comments (0)