API: Windows Services
Functions for getting status, starting, stopping, and restarting Windows services.
C++ registration: regService(js).
Requires JsN.exe to be run as administrator.
Possible service states
State (state) |
Code (code) |
Description |
|---|---|---|
"running" |
4 | Service is running |
"stopped" |
1 | Service is stopped |
"start_pending" |
2 | Start in progress |
"stop_pending" |
3 | Stop in progress |
"paused" |
7 | Paused |
"pause_pending" |
6 | Transitioning to pause |
"continue_pending" |
5 | Resuming |
"unknown" |
— | Unknown state |
svc_status
svc_status(name: string) → object
Get the current state of a service.
Parameters:
| Parameter | Type | Description |
|---|---|---|
name |
string | Service system name (not the display name) |
Result:
| Field | Type | Description |
|---|---|---|
state |
string | State from the table above |
code |
number | Numeric SERVICE_* code (1–7) |
pid |
number | Service process PID (0 if not running) |
error |
string | Error message or "" |
Example:
let s = svc_status("MyService");
if (s.error) {
console.log("Error:", s.error);
} else {
console.log("State:", s.state); // "running"
console.log("PID:", s.pid); // 4321
}
svc_start
svc_start(name: string) → object
Start a service.
Parameters:
| Parameter | Type | Description |
|---|---|---|
name |
string | Service system name |
Result:
| Field | Type | Description |
|---|---|---|
ok |
bool | true — start command sent successfully |
error |
string | Error message or "" |
Example:
let r = svc_start("MyService");
if (r.ok) {
console.log("Service is starting");
} else {
console.log("Start error:", r.error);
}
svc_stop
svc_stop(name: string) → object
Stop a service.
Parameters:
| Parameter | Type | Description |
|---|---|---|
name |
string | Service system name |
Result:
| Field | Type | Description |
|---|---|---|
ok |
bool | true — stop command sent successfully |
error |
string | Error message or "" |
Example:
let r = svc_stop("MyService");
if (r.ok) {
console.log("Service is stopping");
} else {
console.log("Stop error:", r.error);
}
svc_restart
svc_restart(name: string) → object
Restart a service: stop → wait for completion (up to 15 sec) → start.
Parameters:
| Parameter | Type | Description |
|---|---|---|
name |
string | Service system name |
Result:
| Field | Type | Description |
|---|---|---|
ok |
bool | true — service restarted successfully |
error |
string | Error message or "" |
Example:
let r = svc_restart("MyService");
if (r.ok) {
console.log("Service restarted");
} else {
console.log("Restart error:", r.error);
}
Full example: state check and auto-restart
let svcName = "MyService";
// Check state
let s = svc_status(svcName);
if (s.error) {
console.log("[ERROR] Failed to get status:", s.error);
} else if (s.state === "stopped") {
console.log("[WARN] Service is stopped, starting...");
let r = svc_start(svcName);
console.log("Result:", r.ok ? "OK" : r.error);
} else if (s.state === "running") {
console.log("[OK] Service is running, PID:", s.pid);
// Check if the process is responding
let alive = proc_alive(s.pid);
if (!alive.responding) {
console.log("[WARN] Process is hung, restarting service...");
let r = svc_restart(svcName);
console.log("Restart:", r.ok ? "OK" : r.error);
}
} else {
console.log("[INFO] State:", s.state);
}
How to find the service system name
The system name (not the display name) can be found:
- PowerShell:
Get-Service | Where-Object { $_.DisplayName -like "*MyApp*" } | Select-Object Name - Task Manager: Services tab → Name column
- services.msc: right-click → Properties → Service name field