Home / Guides / Keep a program running

Keep a program running on Windows: after reboots, sign-outs, crashes and hangs

A program that has to run all the time can stop in several ways: it crashes, the computer restarts, someone signs out, it hangs, or somebody closes it. Windows has a built-in answer for some of these, and which one fits depends on one question: does the program need a desktop? This guide sorts the options and links to the details.

Short answer.

If the program can run as a Windows service, run it as one: set its start type to Automatic (Delayed Start) so it starts after a reboot with nobody signed in, and set its Recovery actions so Windows starts it again when it crashes.

If it is a background program that cannot be a service, start it with a Task Scheduler task that runs at startup under a service account. If it has a window, it can only run while someone is signed in: start it at logon in that person's session and let a watchdog start it again when it stops. Signing out closes it; disconnecting from Remote Desktop does not.

None of these notices a program that hangs. For that you need a health check.

What stops a program, and what brings it back

What happensWhat brings it back
It crashes or exits A service: its Recovery actions. A program: a watchdog. Task Scheduler's "restart on failure" did not restart a crashed program in our test.
The computer restarts (updates, power) A service: start type Automatic. A background program: a task that runs at startup. A program with a window: a task that runs at logon, so only after someone signs in.
The user signs out Services and startup tasks keep running. Programs in that user's session are closed and start again at the next sign-in.
A Remote Desktop session is disconnected Nothing to do: the session and its programs keep running, unless a policy sets a time limit for disconnected sessions.
It hangs Nothing built in. A health check that stops the frozen process, so the restart above can happen.
Somebody stops or closes it Service Recovery does not react (a normal stop is not a failure). A watchdog starts it again, even when you closed it on purpose.

First decide: does it need a desktop?

Since Windows Vista, services run in session 0, which has no interactive desktop. A task that runs whether or not anyone is logged on has no visible desktop either. So anything that starts without a signed-in user runs where its windows are never seen. That is fine for a console program, a worker or an agent. It is not fine for a program a person has to look at or click.

  • No window needed: use option 1 or 2 below. The program starts at boot, before anyone signs in, and survives sign-outs.
  • Window needed: use option 3. The program runs in a user's session, so it lives and dies with that session.

Option 1: run it as a Windows service

This is the best choice for programs that are written as services or have a service mode (many servers and agents have an install switch; check the program's documentation). Two settings keep a service running:

  • Start type. In services.msc, Automatic starts the service each time the computer starts and runs it even if no one logs on; Automatic (Delayed Start) starts it a short time after the other automatic services, which helps when it depends on them.
  • Recovery. On the service's Recovery tab, choose Restart the Service for the first, second and later failures. Enable actions for stops with errors also covers a service that stops itself with an error code.

The same from an elevated prompt, which is easier to repeat on many servers:

sc.exe config "My Service" start= delayed-auto
sc.exe failure "My Service" reset= 86400 actions= restart/60000/restart/60000/restart/300000
sc.exe failureflag "My Service" 1
sc.exe qc "My Service"
sc.exe qfailure "My Service"

Keep the space after each =; Microsoft's sc.exe documentation says it is required. Delays are in milliseconds and reset= is in seconds (86400 is one day). qc and qfailure show what is set.

Recovery only reacts to a crash: Windows counts a service as failed when its process ends without reporting that it stopped. A clean stop, a stop by a person or a patch installer, and a hung service are not restarted. The details, and a watchdog script for services, are in Windows service recovery vs a watchdog.

Can any program be a service?

Not by registering its .exe with sc.exe create. When Windows starts a service it waits for the program to connect to the Service Control Manager (StartServiceCtrlDispatcher) and report that it is running. An ordinary program never does, so the start fails. A third-party "service wrapper" fills that gap; the program then runs in session 0 as the service account, with no visible window.

Option 2: a background program, started at boot by Task Scheduler

For a console program or worker that is not a service, a task with an At startup trigger that runs under a service account starts it after every reboot, with nobody signed in. From an elevated PowerShell prompt:

$action    = New-ScheduledTaskAction -Execute 'C:\Program Files\MyWorker\MyWorker.exe'
$trigger   = New-ScheduledTaskTrigger -AtStartup
$principal = New-ScheduledTaskPrincipal -UserId 'LOCALSERVICE' -LogonType ServiceAccount
$settings  = New-ScheduledTaskSettingsSet -ExecutionTimeLimit ([TimeSpan]::Zero) -MultipleInstances IgnoreNew `
                 -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
Register-ScheduledTask -TaskName 'Start MyWorker' -Action $action -Trigger $trigger -Principal $principal -Settings $settings
  • -AtStartup: the task starts when the computer starts.
  • -UserId 'LOCALSERVICE' -LogonType ServiceAccount: the task runs as the Local Service account, which has minimum privileges on the computer and presents anonymous credentials on the network. Choose the Network Service account if the program needs to reach other computers as this computer, and LocalSystem only if it really needs full local rights.
  • -ExecutionTimeLimit ([TimeSpan]::Zero): no time limit. By default Task Scheduler stops a task 72 hours after it starts.
  • -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries and -MultipleInstances IgnoreNew: as explained in the crash guide.

The settings are the ones tested in the crash guide; the at-startup trigger and the Local Service principal are Microsoft's documented parameters. After the first restart, Get-ScheduledTaskInfo -TaskName 'Start MyWorker' shows when it last ran and its result.

The limit: the task starts the program once per boot. If the program exits, it stays down until the next restart, because Task Scheduler's "restart on failure" setting does not treat a crash as a task failure. To restart it as well, make the task's action the watchdog script from the crash guide instead of the program itself, the same way that guide's logon task does, and give it a -LogFile path the task's account can write to.

Option 3: a program with a window, in your own session

A program people need to see must run in a signed-in user's session. Start it with a Task Scheduler task that runs at logon with the logon type Run only when user is logged on, and let a watchdog in the same session start it again when it stops. The tested watchdog and the task that starts it are in Restart a program when it crashes.

After a reboot nobody is signed in

Until someone signs in, nothing in a user session runs. On a dedicated, physically secured machine (a wall display, a kiosk, a lab PC), Windows automatic sign-in closes that gap. Microsoft warns that it is a security risk: anyone with physical access gets the account, and when it is set in the registry the password is stored there in plain text, readable remotely by authenticated users. The Sysinternals Autologon tool stores it as an LSA secret instead. Do not use automatic sign-in on a server other people can reach.

Sign out, lock or disconnect

  • Signing out ends every program in your session, including the watchdog. They start again at your next sign-in.
  • Locking the screen (Windows+L) ends nothing.
  • Disconnecting from a Remote Desktop session keeps the session and its programs running. By default disconnected sessions are kept for an unlimited time, but the Group Policy setting Set time limit for disconnected sessions deletes them after a set time, and the idle and active session limits can disconnect or end a session too. On a shared server, check those policies under Remote Desktop Services, Remote Desktop Session Host, Session Time Limits before you rely on it.

Hangs: running is not the same as working

Every option above restarts a program that is gone. A frozen program still has a process, and a hung service still says Running, so neither Windows nor a watchdog sees a problem. You need a check that the program is doing its job: whether its window responds, or a signal from the program itself such as a log file that keeps changing or a port that answers. When the check fails several times in a row, stop the process and let the restart do the rest. How to do that is in the crash guide's section on hangs.

Which option fits

  • A program that is, or can install itself as, a service: option 1. It starts at boot and Windows restarts it after a crash.
  • A background program with no service mode: option 2, with the watchdog as the task's action if it must also come back after a crash.
  • A program with a window: option 3. Add automatic sign-in only on a dedicated machine nobody else can reach.
  • All of them: add a health check if the program can hang, and some way to hear about restarts; Windows tells nobody.

Questions

How do I keep a program running after a restart without signing in?

Run it as a Windows service with the start type Automatic, or start it with a Task Scheduler task that has an At startup trigger and runs under a service account. Both run without anyone signed in, in session 0, so the program has no visible window. A program that needs a window needs a signed-in user.

Does a program keep running when I sign out?

No. Signing out ends every program in your session. Services and tasks that run at startup under a service account keep running. Lock the screen, or disconnect from Remote Desktop, instead of signing out.

Does a program keep running after I disconnect from Remote Desktop?

Yes, by default. A disconnected session and its programs stay active for an unlimited time, unless a Group Policy setting such as Set time limit for disconnected sessions ends it.

Can I turn any .exe into a Windows service?

Not with sc.exe create alone. Windows waits for a service program to connect to the Service Control Manager and report that it is running; an ordinary program never does, so the start fails. Use the program's own service mode if it has one, a service wrapper, or a task that runs at startup.

Will Windows restart a program that hangs?

No. A hung program still has a process and a hung service still reports Running, so neither the service Recovery settings nor a watchdog that checks the process sees a problem. Add a health check, as described in Restart a program when it crashes.

Related

Sources (Microsoft Learn): sc.exe config, SERVICE_FAILURE_ACTIONS, SERVICE_FAILURE_ACTIONS_FLAG, Service startup, StartServiceCtrlDispatcher, Service changes for Windows Vista (session 0), New-ScheduledTaskTrigger, New-ScheduledTaskPrincipal, New-ScheduledTaskSettingsSet, Get-ScheduledTaskInfo, Security contexts for tasks, LocalService account, NetworkService account, Logging off, LogonType (tasks), Remote Desktop session time limits (ADMX_TerminalServer), Turn on automatic logon in Windows.

An unhandled error has occurred. 🗙