2018-11-29 16:00:00 +00:00
# Self-restarting Windows services
2016-11-24 03:02:30 +00:00
2018-11-27 16:00:00 +00:00
## Restart from the spawned process
2016-11-24 03:02:30 +00:00
2020-08-10 23:54:32 +00:00
To support self-restarting services, winsw exposes `WINSW_EXECUTABLE` environment variable into the forked process,
2018-11-29 16:00:00 +00:00
which refers to the full path of *WinSW.exe* that's managing the service.
2020-08-10 23:54:32 +00:00
To restart the service from within, execute `%WINSW_EXECUTABLE% restart!` .
Note that you are invoking `restart!` command, not `restart` command.
This hidden command is a flavor of the `restart` operation,
where winsw creates another winsw process in a separate process group,
2016-11-24 03:02:30 +00:00
and restarts the service from there.
2020-08-10 23:54:32 +00:00
This additional indirection is necessary because WinSW will kill child processes recursively when it stops a service.
Windows Service Control Manager (SCM) doesn't provide the restart operation as an atomic operation either, so winsw implements restart by a sequence of stop and start.
2018-11-29 16:00:00 +00:00
The second winsw process in a separate process group ensures that winsw can survive this massacre to execute the start call.