🧠 How to Run a Celery Worker as a Windows Service Using NSSM

🧠 How to Run a Celery Worker as a Windows Service Using NSSM

Because dev life on Windows deserves smooth background workers too.


πŸ’­ The Problem

So you're working on a Django or Flask project. You’ve set up Celery to handle background tasks — perfect for sending emails, crunching data, or queuing long-running jobs. Everything works great locally… until you close your terminal or restart your PC.

Suddenly, your Celery worker disappears.

Welcome to the reality of running Python apps on Windows. But don’t worry — there's a simple fix: run Celery as a Windows Service using NSSM.

Let’s walk through the full process step-by-step.


🧰 What You’ll Need

Before diving in, make sure you’ve got the essentials:


πŸ›  Step 1: Install NSSM

First up, grab a copy of NSSM.

  1. Head over to nssm.cc/download

  2. Download the version that matches your OS (likely 64-bit)

  3. Unzip it

  4. Copy the correct nssm.exe (from the win64 or win32 folder) to somewhere on your PATH — like C:\Windows\System32
    Or just keep it in a known folder and reference it directly


πŸͺŸ Step 2: Run Command Prompt as Admin

You’ll need admin privileges to register a service.

  • Hit Win → search “cmd”

  • Right-click it → select Run as administrator

πŸ“Œ If you skip this step, NSSM won’t be able to install the service, and you’ll hit permission errors later.


πŸ” Step 3: Figure Out Your Celery Command

In your virtual environment, you typically run Celery with a command like:

celery -A dj worker --loglevel=info

But for reliability, especially in a service context, run it through Python like this:

D:\newdj.com\env\Scripts\python.exe -m celery -A dj worker --loglevel=info

Why? This makes sure the right environment is loaded — no ambiguity about what celery is pointing to.


πŸ§‘‍πŸ”§ Step 4: Create the Celery Service with NSSM

From your admin terminal, type:

nssm install dj_dev_worker

A small GUI window will pop up — this is where you define how the service runs.


βœ… Application Tab

Fill in the following:

Field Value
Path D:\newdj.com\env\Scripts\python.exe
Arguments -m celery -A dj worker --loglevel=info
Startup Directory D:\newdj.com\development

πŸ“„ I/O Tab – Logging

Set up log files so you can check what Celery is doing.

Field Path
stdout C:\Logs\dj_dev_worker_output.log
stderr C:\Logs\dj_dev_worker_error.log

⚠️ If the C:\Logs\ folder doesn't exist, create it first — NSSM won’t do it for you, and the service will silently fail otherwise.


πŸ“ Details Tab (Optional)

Just for visibility:

Field Value
Display Name dj Celery Worker Dev
Description Runs Celery Worker for dev purposes

Once you’re done, click Install service at the bottom.


▢️ Step 5: Start the Service

With the service installed, you can start it in two ways:

Option 1: From CMD

nssm start dj_dev_worker

Option 2: From the Windows Services GUI

  • Press Win + R → type services.msc → hit Enter

  • Find dj_dev_worker in the list

  • Right-click → Start

You should now see its status change to Running βœ…


πŸ•΅οΈ Step 6: Check If It’s Working

Open the output log you set earlier:

C:\Logs\dj_dev_worker_output.log

You should see logs like:

[INFO/MainProcess] Connected to amqp://guest@localhost:5672//
[INFO/MainProcess] celery@DESKTOP ready.

Boom — your Celery worker is now running in the background as a real Windows service. πŸŽ‰


🧭 Bonus: Controlling the Service

Here are the basic commands you’ll use again and again:

nssm start dj_dev_worker    # Start
nssm stop dj_dev_worker     # Stop
nssm restart dj_dev_worker  # Restart

For pausing/resuming, use the Services GUI (services.msc) — but note, not all services (like Celery) respond to pause signals.


πŸ” Optional: Auto-Restart on Failure

You can make your service more resilient by auto-restarting if it crashes.

  1. Run:

    nssm edit dj_dev_worker
    
  2. Go to the Exit Actions tab

  3. Set:

    • On exit: Restart service

    • Delay: 3000 ms (or whatever works for you)


πŸ‘€ Advanced: Run Under a Specific User

By default, Windows services run as Local System — fine for most cases. But if your worker needs access to files on the network, mapped drives, or user-specific permissions, do this:

  1. Open services.msc

  2. Find your service → double-click it

  3. Go to Log On tab

  4. Select This account → enter your username + password

Done. The service now runs under your user context.


βœ… Wrapping Up

With NSSM, you’ve turned Celery into a true Windows service. That means:

  • No more leaving terminals open

  • Clean start/stop/restart via GUI or command line

  • Automatic log storage

  • Optional restart-on-failure

  • Closely mirrors how Celery runs in production

Whether you're debugging tasks or building a microservices setup, this approach keeps your dev environment reliable — even on Windows.


πŸ§ͺ TL;DR

nssm install dj_dev_worker
# Set:
# Path: python.exe from your venv
# Args: -m celery -A dj worker --loglevel=info
# Dir: Your project folder
# Logs: Setup stdout/stderr paths

nssm start dj_dev_worker

That's it — you now have a background Celery worker, ready to go.

Bonus: Want to run Celery Beat too? Just repeat this process with a different name and -B or beat argument.




πŸ”„ Edit, Rename or Remove a Service

If you accidentally named your service wrong or no longer need it, you can easily remove it with NSSM:

nssm remove <service_name> confirm

Example:

nssm remove dj_worker_typo confirm

βœ… The confirm flag ensures the service is deleted without an extra confirmation prompt.

πŸ”§ Alternative: Remove Manually

If you’re not using NSSM anymore or need a manual method, use the built-in sc tool:

sc delete <service_name>

But first, make sure the service is stopped:

nssm stop <service_name>


✏️ Renaming a Service (No Direct Command)

Windows doesn't support directly renaming a service, but here's a simple workaround:

  1. Install a new service with the correct name:

    nssm install new_service_name

    Re-enter the same settings you used previously (path, arguments, I/O, etc).

  2. Remove the old service:

    nssm remove old_service_name confirm

  3. Tweak and verify your new service:

    nssm edit new_service_name

    βœ… Double-check your application path, arguments, I/O redirection, and environment variables.


πŸŽ‰ And you're done! Your service is now up and running under its new, correct name — no registry hacks, no complexity. Just clean, reliable NSSM magic.


πŸ“¦ Unofficial Source Code

This guide uses the excellent imvickykumar999/Non-Sucking-Service-Manager, an open-source Windows service wrapper for running any executable as a service.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Loading...