Run a Python Crypto Bot as a Windows Service with NSSM

Turn your Python crypto trading bot into a Windows service that starts at boot, restarts after crashes and keeps running after you close RDP.

dns WindowsVPS Team calendar_today schedule5 min read
Run a Python Crypto Bot as a Windows Service with NSSM
tocOn this page 8 sections
  1. Why a service beats a console window
  2. 1. Install Python for all users and create a venv
  3. 2. Create a low-privilege service account
  4. 3. Register the service with NSSM
  5. 4. Keep API keys out of the service config
  6. 5. Make the bot restart-safe
  7. 6. Verify it survives real failures
  8. FAQ

To run a Python crypto trading bot as a Windows service, install it in its own virtual environment, wrap the venv's python.exe with a service manager such as NSSM, and configure the service to start automatically, restart on failure and write its output to log files. The bot then keeps trading through RDP logoffs, crashes and Windows Update reboots, and no one has to sign in.

Why a service beats a console window

A common setup is to start the bot in a PowerShell window over RDP and then disconnect. That works until the session is logged off, the VPS reboots for updates, or an unhandled exchange error kills the script at 3 a.m. A Windows service runs outside any user session and starts before anyone logs in. The Service Control Manager can also restart it automatically. Python cannot register itself as a service out of the box, so a small wrapper handles that part.

1. Install Python for all users and create a venv

Run the Python installer with the option to install for all users. A per-user install lands in your profile, where a service account may not be able to read it. Then give the bot its own folder and virtual environment:

New-Item -ItemType Directory C:\bots\gridbot\logs -Force
Set-Location C:\bots\gridbot
py -m venv .venv
.\.venv\Scripts\python.exe -m pip install -r requirements.txt

Always reference the venv interpreter by its full path. Services do not pick up your interactive PATH. Relying on plain python is the usual reason a bot runs fine in a console and then fails as a service.

2. Create a low-privilege service account

Do not run a bot that holds exchange keys as LocalSystem. Create a local account and give it read access to the code and write access only to the logs folder:

$pw = Read-Host -AsSecureString "Password for svc-bot"
New-LocalUser -Name svc-bot -Password $pw -PasswordNeverExpires
icacls C:\bots\gridbot /grant "svc-bot:(OI)(CI)RX"
icacls C:\bots\gridbot\logs /grant "svc-bot:(OI)(CI)M"

If the service later fails with a logon error, check that the account holds the Log on as a service right in Local Security Policy.

3. Register the service with NSSM

NSSM (the Non-Sucking Service Manager) is a single free executable. Copy nssm.exe to a folder such as C:\Tools, then run these commands from an elevated PowerShell prompt:

$svc = "GridBot"
nssm install $svc C:\bots\gridbot\.venv\Scripts\python.exe "-u bot.py"
nssm set $svc AppDirectory C:\bots\gridbot
nssm set $svc AppStdout C:\bots\gridbot\logs\service.log
nssm set $svc AppStderr C:\bots\gridbot\logs\service.log
nssm set $svc AppRotateFiles 1
nssm set $svc AppRotateOnline 1
nssm set $svc AppRotateBytes 10485760
nssm set $svc Start SERVICE_DELAYED_AUTO_START
nssm set $svc AppExit Default Restart
nssm set $svc AppRestartDelay 15000
nssm edit $svc

Here is what the settings do:

  • -u turns off output buffering, so log lines reach the file immediately instead of in delayed chunks.
  • Delayed auto start gives networking and time sync a moment to come up after boot.
  • AppRestartDelay waits 15 seconds before a restart. A crash loop therefore cannot hammer the exchange API and trip rate limits.

The final nssm edit command opens a GUI. On its Log on tab, enter the svc-bot credentials. That keeps the password out of your PowerShell history. Start the service with nssm start GridBot.

NSSM is not the only option. WinSW uses an XML config file, and pywin32 lets you write a native service class in Python. The principles below apply whichever wrapper you pick. On a Linux VPS, a systemd unit with Restart=on-failure does the same job.

4. Keep API keys out of the service config

You can pass keys through NSSM's environment settings, but those values are stored as plain text in the registry. A better approach is to load them from a .env file that only svc-bot and Administrators can read. On the exchange side:

  • Create keys with trading permission only. Never enable withdrawals.
  • Restrict each key to your VPS's static IP address if the exchange supports it.
  • Lock down the server itself. The checklist in our Windows Server VPS hardening guide covers RDP exposure, firewall rules and updates.

5. Make the bot restart-safe

Automatic restarts only help if the bot behaves correctly when it comes back up.

  • Graceful stop: when stopping a service, NSSM first sends a console Ctrl+C. Catch KeyboardInterrupt, close websockets, save state and exit cleanly.
  • Persist state: write open orders and positions to a file or SQLite database. On startup, reconcile that state with the exchange instead of assuming a clean slate. Otherwise a restart can place duplicate orders.
  • Clock drift: exchanges reject signed requests whose timestamps fall outside their allowed window. Check the clock with w32tm /query /status and force a sync with w32tm /resync if needed.

6. Verify it survives real failures

Test the service before you trust it with money:

Get-Service GridBot
Get-Content C:\bots\gridbot\logs\service.log -Tail 50 -Wait
# Simulate a crash: find the bot's PID and kill it, then watch it come back
Get-CimInstance Win32_Process -Filter "Name='python.exe'" | Select ProcessId, CommandLine
Restart-Computer

After the reboot, reconnect over RDP and confirm that the log shows a fresh startup without you signing in first. If the service will not start, look for errors in the System event log from the Service Control Manager. Also make sure the bot's state database is included in your regular backups.

For hardware sizing and data center choice, see our page on Windows VPS plans for crypto trading bots.

FAQ

Will the bot keep running after I close Remote Desktop?

Yes. A service does not depend on any user session, so disconnecting or logging off RDP has no effect on it.

Can I run several bots on one VPS?

Yes. Give each bot its own folder, virtual environment, log file and service name, so a bad dependency upgrade in one cannot break the others. Watch RAM use as you add bots.

Why not just use Task Scheduler at startup?

Task Scheduler can launch a script at boot. A service manager adds restart-on-crash, log capture and rotation, and clean stop handling, and those features matter for a bot that trades around the clock.

Run this on your own Windows VPS

Get a Windows VPS from WindowsVPS in about 60 seconds, with full Administrator access over RDP, NVMe storage and the Windows license included.

Buy a Windows VPS →

dns

Written by the WindowsVPS Team

We run Windows Server VPS hosting and write these guides from the same setups our customers use: RDP, PowerShell, trading terminals and always-on bots. Questions about a guide? Contact us.

About WindowsVPS Windows VPS guides All articles

Ready to deploy your server?

Windows or Linux VPS — up and running in under 60 seconds.