π§ 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:
-
A Python project with Celery configured
-
A working virtual environment (with Celery installed)
-
Admin access to your Windows machine
π Step 1: Install NSSM
First up, grab a copy of NSSM.
-
Head over to nssm.cc/download
-
Download the version that matches your OS (likely 64-bit)
-
Unzip it
-
Copy the correct
nssm.exe(from thewin64orwin32folder) to somewhere on yourPATH— likeC:\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→ typeservices.msc→ hit Enter -
Find
dj_dev_workerin 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.
-
Run:
nssm edit dj_dev_worker -
Go to the Exit Actions tab
-
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:
-
Open
services.msc -
Find your service → double-click it
-
Go to Log On tab
-
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:
-
Install a new service with the correct name:
nssm install new_service_nameRe-enter the same settings you used previously (path, arguments, I/O, etc).
-
Remove the old service:
nssm remove old_service_name confirm -
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