How to Run Background Processes in Windows: Equivalent of nohup for Windows Developers


3 views

While Unix-like systems have nohup to run processes in the background, Windows offers several alternatives with different levels of control:

1. Using START command:

START /B your_command.exe

2. PowerShell background jobs:

Start-Job -ScriptBlock { your_command.exe }

3. Windows Services (for persistent processes):

sc create MyService binPath= "C:\path\to\your\program.exe"

For closer UNIX-like behavior:

1. Using Windows Subsystem for Linux (WSL):

wsl nohup your_command &

2. NSSM (Non-Sucking Service Manager):

nssm install MyService "C:\path\to\your\program.exe"

For a Python script that should run continuously:

START /B python myscript.py > output.log 2>&1

Or using PowerShell:

$job = Start-Job -ScriptBlock { python myscript.py }
Receive-Job $job -Wait

To capture output like nohup does:

START /B your_command.exe > output.log 2>&1

For PowerShell jobs, output is automatically captured and can be retrieved with:

Receive-Job -Job $job

For processes that should survive terminal closure:

Using SCHTASKS:

schtasks /create /tn "MyTask" /tr "C:\path\to\program.exe" /sc ONLOGON

When transitioning from Unix-like systems to Windows, many developers miss the convenience of nohup for running persistent background processes. Windows handles process management differently, but offers several robust alternatives.

The most straightforward approaches using built-in Windows features:

# Using START command
START /B your_command.exe

# For detached processes
START "" /B your_command.exe > output.log 2>&1

PowerShell provides more sophisticated control:

# Run process in background job
$job = Start-Job -ScriptBlock { your_command.exe }

# Check job status
Get-Job

# Retrieve output
Receive-Job $job -Keep

For permanent background processes, consider creating a Windows Service:

# Using sc command to create service
sc create MyService binPath= "C:\path\to\your\program.exe" start= auto

Several utilities can provide *nix-like behavior:

  • NSSM (Non-Sucking Service Manager)
  • AlwaysUp (commercial tool)
  • WinSW (Windows Service Wrapper)

Here's how to run a Python script persistently:

# Using Pythonw (no console window)
pythonw.exe your_script.py

# As a scheduled task (persistent after logout)
schtasks /create /tn "MyPythonTask" /tr "python your_script.py" /sc onstart

To capture output like nohup does:

# Redirect all output to file
your_command.exe > output.log 2>&1

# Using PowerShell's Tee-Object
your_command.exe | Tee-Object -FilePath output.log