Bearsampp
2026.9.23
Toggle main menu visibility
Loading...
Searching...
No Matches
class.nssm.php
Go to the documentation of this file.
1
<?php
2
/*
3
* Copyright (c) 2021-2026 Bearsampp
4
* License: GNU General Public License version 3 or later; see LICENSE.txt
5
* Author: Bear
6
* Website: https://bearsampp.com
7
* Github: https://github.com/Bearsampp
8
*/
9
17
class
Nssm
18
{
19
// Start params
20
const
SERVICE_AUTO_START
=
'SERVICE_AUTO_START'
;
21
const
SERVICE_DELAYED_START
=
'SERVICE_DELAYED_START'
;
22
const
SERVICE_DEMAND_START
=
'SERVICE_DEMAND_START'
;
23
const
SERVICE_DISABLED
=
'SERVICE_DISABLED'
;
24
25
// Type params
26
const
SERVICE_WIN32_OWN_PROCESS
=
'SERVICE_WIN32_OWN_PROCESS'
;
27
const
SERVICE_INTERACTIVE_PROCESS
=
'SERVICE_INTERACTIVE_PROCESS'
;
28
29
// Status
30
const
STATUS_CONTINUE_PENDING
=
'SERVICE_CONTINUE_PENDING'
;
31
const
STATUS_PAUSE_PENDING
=
'SERVICE_PAUSE_PENDING'
;
32
const
STATUS_PAUSED
=
'SERVICE_PAUSED'
;
33
const
STATUS_RUNNING
=
'SERVICE_RUNNING'
;
34
const
STATUS_START_PENDING
=
'SERVICE_START_PENDING'
;
35
const
STATUS_STOP_PENDING
=
'SERVICE_STOP_PENDING'
;
36
const
STATUS_STOPPED
=
'SERVICE_STOPPED'
;
37
const
STATUS_NOT_EXIST
=
'SERVICE_NOT_EXIST'
;
38
const
STATUS_NA
=
'-1'
;
39
40
// Infos keys
41
const
INFO_APP_DIRECTORY
=
'AppDirectory'
;
42
const
INFO_APPLICATION
=
'Application'
;
43
const
INFO_APP_PARAMETERS
=
'AppParameters'
;
44
const
INFO_APP_STDERR
=
'AppStderr'
;
45
const
INFO_APP_STDOUT
=
'AppStdout'
;
46
const
INFO_APP_ENVIRONMENT_EXTRA
=
'AppEnvironmentExtra'
;
47
48
const
PENDING_TIMEOUT
= 10;
49
const
SLEEP_TIME
= 100000;
50
51
private
$name
;
52
private
$displayName
;
53
private
$binPath
;
54
private
$params
;
55
private
$start
;
56
private
$stdout
;
57
private
$stderr
;
58
private
$environmentExtra
;
59
private
$latestError
;
60
private
$latestStatus
;
61
68
public
function
__construct
(
$name
)
69
{
70
Log::initClass
($this);
71
$this->name =
$name
;
72
}
73
79
private
function
writeLog
($log)
80
{
81
global
$bearsamppRoot
;
82
Log::debug
($log,
Path::getNssmLogFilePath
());
83
}
84
90
private
function
writeLogInfo
($log)
91
{
92
global
$bearsamppRoot
;
93
Log::info
($log,
Path::getNssmLogFilePath
());
94
}
95
101
private
function
writeLogError
($log)
102
{
103
global
$bearsamppRoot
;
104
Log::error
($log,
Path::getNssmLogFilePath
());
105
}
106
114
private
function
exec
($args)
115
{
116
global
$bearsamppCore
;
117
118
$command
=
'"'
.
Path::getNssmExe
() .
'" '
. $args;
119
$this->
writeLogInfo
(
'Cmd: '
.
$command
);
120
121
$result
=
Batch::exec
(
'nssm'
,
$command
, 10);
122
if
(is_array(
$result
)) {
123
$rebuildResult = array();
124
foreach
(
$result
as $row) {
125
$row = trim($row);
126
if
(!empty($row)) {
127
$rebuildResult[] = preg_replace(
'/[\x00-\x1F\x80-\xFF]/'
,
''
, $row);
128
}
129
}
130
$result
= $rebuildResult;
131
if
(count(
$result
) > 1) {
132
$this->latestError = implode(
' ; '
,
$result
);
133
}
134
135
return
$result
;
136
}
137
138
return
false
;
139
}
140
148
public
function
status
($timeout =
true
)
149
{
150
$this->latestStatus = self::STATUS_NA;
151
$maxtime = time() + self::PENDING_TIMEOUT;
152
153
while
($this->latestStatus == self::STATUS_NA || $this->
isPending
($this->latestStatus)) {
154
$exec = $this->
exec
(
'status '
. $this->
getName
());
155
if
($exec !==
false
) {
156
if
(count($exec) > 1) {
157
$this->latestStatus = self::STATUS_NOT_EXIST;
158
}
else
{
159
$this->latestStatus = $exec[0];
160
}
161
}
162
if
($timeout && $maxtime < time()) {
163
break
;
164
}
165
166
// Only sleep if we're going to loop again
167
if
($this->latestStatus == self::STATUS_NA || $this->
isPending
($this->latestStatus)) {
168
usleep(self::SLEEP_TIME);
169
}
170
}
171
172
if
($this->latestStatus == self::STATUS_NOT_EXIST) {
173
$this->latestError =
'Error 3: The specified service does not exist as an installed service.'
;
174
$this->latestStatus = self::STATUS_NA;
175
}
176
177
return
$this->latestStatus
;
178
}
179
185
public
function
create
()
186
{
187
$this->
writeLog
(
'Create service'
);
188
$this->
writeLog
(
'-> service: '
. $this->
getName
());
189
$this->
writeLog
(
'-> display: '
. $this->
getDisplayName
());
190
$this->
writeLog
(
'-> description: '
. $this->
getDisplayName
());
191
$this->
writeLog
(
'-> path: '
. $this->
getBinPath
());
192
$this->
writeLog
(
'-> params: '
. $this->
getParams
());
193
$this->
writeLog
(
'-> stdout: '
. $this->
getStdout
());
194
$this->
writeLog
(
'-> stderr: '
. $this->
getStderr
());
195
$this->
writeLog
(
'-> environment extra: '
. $this->
getEnvironmentExtra
());
196
$this->
writeLog
(
'-> start_type: '
. ($this->
getStart
() !=
null
? $this->
getStart
() : self::SERVICE_DEMAND_START));
197
198
// Install bin
199
$exec = $this->
exec
(
'install '
. $this->
getName
() .
' "'
. $this->
getBinPath
() .
'"'
);
200
if
($exec ===
false
) {
201
return
false
;
202
}
203
204
// Params
205
$exec = $this->
exec
(
'set '
. $this->
getName
() .
' AppParameters "'
. $this->
getParams
() .
'"'
);
206
if
($exec ===
false
) {
207
return
false
;
208
}
209
210
// DisplayName
211
$exec = $this->
exec
(
'set '
. $this->
getName
() .
' DisplayName "'
. $this->
getDisplayName
() .
'"'
);
212
if
($exec ===
false
) {
213
return
false
;
214
}
215
216
// Description
217
$exec = $this->
exec
(
'set '
. $this->
getName
() .
' Description "'
. $this->
getDisplayName
() .
'"'
);
218
if
($exec ===
false
) {
219
return
false
;
220
}
221
222
// No AppNoConsole to fix nssm problems with Windows 10 Creators update.
223
$exec = $this->
exec
(
'set '
. $this->
getName
() .
' AppNoConsole "1"'
);
224
if
($exec ===
false
) {
225
return
false
;
226
}
227
228
// Start
229
$exec = $this->
exec
(
'set '
. $this->
getName
() .
' Start "'
. ($this->
getStart
() !=
null
? $this->
getStart
() : self::SERVICE_DEMAND_START) .
'"'
);
230
if
($exec ===
false
) {
231
return
false
;
232
}
233
234
// Stdout
235
$exec = $this->
exec
(
'set '
. $this->
getName
() .
' AppStdout "'
. $this->
getStdout
() .
'"'
);
236
if
($exec ===
false
) {
237
return
false
;
238
}
239
240
// Stderr
241
$exec = $this->
exec
(
'set '
. $this->
getName
() .
' AppStderr "'
. $this->
getStderr
() .
'"'
);
242
if
($exec ===
false
) {
243
return
false
;
244
}
245
246
// Environment Extra
247
$exec = $this->
exec
(
'set '
. $this->
getName
() .
' AppEnvironmentExtra '
. $this->
getEnvironmentExtra
());
248
if
($exec ===
false
) {
249
return
false
;
250
}
251
252
if
(!$this->
isInstalled
()) {
253
$this->latestError =
null
;
254
255
return
false
;
256
}
257
258
return
true
;
259
}
260
266
public
function
delete
()
267
{
268
$this->
stop
();
269
270
$this->
writeLog
(
'Delete service '
. $this->
getName
());
271
$exec = $this->
exec
(
'remove '
. $this->
getName
() .
' confirm'
);
272
if
($exec ===
false
) {
273
return
false
;
274
}
275
276
if
($this->
isInstalled
()) {
277
$this->latestError =
null
;
278
279
return
false
;
280
}
281
282
return
true
;
283
}
284
290
public
function
start
()
291
{
292
$this->
writeLog
(
'Start service '
. $this->
getName
());
293
294
$exec = $this->
exec
(
'start '
. $this->
getName
());
295
if
($exec ===
false
) {
296
return
false
;
297
}
298
299
if
(!$this->
isRunning
()) {
300
$this->latestError =
null
;
301
302
return
false
;
303
}
304
305
return
true
;
306
}
307
313
public
function
stop
()
314
{
315
$this->
writeLog
(
'Stop service '
. $this->
getName
());
316
317
$exec = $this->
exec
(
'stop '
. $this->
getName
());
318
if
($exec ===
false
) {
319
return
false
;
320
}
321
322
if
(!$this->
isStopped
()) {
323
$this->latestError =
null
;
324
325
return
false
;
326
}
327
328
return
true
;
329
}
330
336
public
function
restart
()
337
{
338
if
($this->
stop
()) {
339
return
$this->
start
();
340
}
341
342
return
false
;
343
}
344
350
public
function
infos
()
351
{
352
global $bearsamppRegistry;
353
354
$infos =
Win32Native::getServiceInfo
($this->
getName
());
355
if
($infos ===
false
) {
356
return
false
;
357
}
358
359
$infosNssm = array();
360
$infosKeys = array(
361
self::INFO_APPLICATION,
362
self::INFO_APP_PARAMETERS,
363
);
364
365
foreach
($infosKeys as $infoKey) {
366
$value =
null
;
367
$exists = $bearsamppRegistry->exists(
368
Registry::HKEY_LOCAL_MACHINE
,
369
'SYSTEM\CurrentControlSet\Services\\'
. $this->
getName
() .
'\Parameters'
,
370
$infoKey
371
);
372
if
($exists) {
373
$value = $bearsamppRegistry->getValue(
374
Registry::HKEY_LOCAL_MACHINE
,
375
'SYSTEM\CurrentControlSet\Services\\'
. $this->
getName
() .
'\Parameters'
,
376
$infoKey
377
);
378
}
379
$infosNssm[$infoKey] = $value;
380
}
381
382
if
(!isset($infosNssm[self::INFO_APPLICATION])) {
383
return
$infos;
384
}
385
386
$infos[
Win32Service::VBS_PATH_NAME
] = $infosNssm[
Nssm::INFO_APPLICATION
] .
' '
. $infosNssm[
Nssm::INFO_APP_PARAMETERS
];
387
388
return
$infos;
389
}
390
396
public
function
isInstalled
()
397
{
398
$status = $this->
status
();
399
$this->
writeLog
(
'isInstalled '
. $this->
getName
() .
': '
. ($status != self::STATUS_NA ?
'YES'
:
'NO'
) .
' (status: '
. $status .
')'
);
400
401
return
$status != self::STATUS_NA;
402
}
403
409
public
function
isRunning
()
410
{
411
$status = $this->
status
();
412
$this->
writeLog
(
'isRunning '
. $this->
getName
() .
': '
. ($status == self::STATUS_RUNNING ?
'YES'
:
'NO'
) .
' (status: '
. $status .
')'
);
413
414
return
$status == self::STATUS_RUNNING;
415
}
416
422
public
function
isStopped
()
423
{
424
$status = $this->
status
();
425
$this->
writeLog
(
'isStopped '
. $this->
getName
() .
': '
. ($status == self::STATUS_STOPPED ?
'YES'
:
'NO'
) .
' (status: '
. $status .
')'
);
426
427
return
$status == self::STATUS_STOPPED;
428
}
429
435
public
function
isPaused
()
436
{
437
$status = $this->
status
();
438
$this->
writeLog
(
'isPaused '
. $this->
getName
() .
': '
. ($status == self::STATUS_PAUSED ?
'YES'
:
'NO'
) .
' (status: '
. $status .
')'
);
439
440
return
$status == self::STATUS_PAUSED;
441
}
442
450
public
function
isPending
($status)
451
{
452
return
$status == self::STATUS_START_PENDING || $status == self::STATUS_STOP_PENDING
453
|| $status == self::STATUS_CONTINUE_PENDING || $status == self::STATUS_PAUSE_PENDING;
454
}
455
463
private
function
getServiceStatusDesc
($status)
464
{
465
switch
($status) {
466
case
self::STATUS_CONTINUE_PENDING:
467
return
'The service continue is pending.'
;
468
469
case
self::STATUS_PAUSE_PENDING:
470
return
'The service pause is pending.'
;
471
472
case
self::STATUS_PAUSED:
473
return
'The service is paused.'
;
474
475
case
self::STATUS_RUNNING:
476
return
'The service is running.'
;
477
478
case
self::STATUS_START_PENDING:
479
return
'The service is starting.'
;
480
481
case
self::STATUS_STOP_PENDING:
482
return
'The service is stopping.'
;
483
484
case
self::STATUS_STOPPED:
485
return
'The service is not running.'
;
486
487
case
self::STATUS_NA:
488
return
'Cannot retrieve service status.'
;
489
490
default
:
491
return
null
;
492
}
493
}
494
500
public
function
getName
()
501
{
502
return
$this->name
;
503
}
504
510
public
function
setName
(
$name
)
511
{
512
$this->name =
$name
;
513
}
514
520
public
function
getDisplayName
()
521
{
522
return
$this->displayName
;
523
}
524
530
public
function
setDisplayName
(
$displayName
)
531
{
532
$this->displayName =
$displayName
;
533
}
534
540
public
function
getBinPath
()
541
{
542
return
$this->binPath
;
543
}
544
550
public
function
setBinPath
(
$binPath
)
551
{
552
$this->binPath = str_replace(
'"'
,
''
,
Path::formatWindowsPath
(
$binPath
));
553
}
554
560
public
function
getParams
()
561
{
562
return
$this->params
;
563
}
564
570
public
function
setParams
(
$params
)
571
{
572
$this->params =
$params
;
573
}
574
580
public
function
getStart
()
581
{
582
return
$this->start
;
583
}
584
590
public
function
setStart
(
$start
)
591
{
592
$this->
start
=
$start
;
593
}
594
600
public
function
getStdout
()
601
{
602
return
$this->stdout
;
603
}
604
610
public
function
setStdout
(
$stdout
)
611
{
612
$this->stdout =
$stdout
;
613
}
614
620
public
function
getStderr
()
621
{
622
return
$this->stderr
;
623
}
624
630
public
function
setStderr
(
$stderr
)
631
{
632
$this->stderr =
$stderr
;
633
}
634
640
public
function
getEnvironmentExtra
()
641
{
642
return
$this->environmentExtra
;
643
}
644
650
public
function
setEnvironmentExtra
(
$environmentExtra
)
651
{
652
$this->environmentExtra =
Path::formatWindowsPath
(
$environmentExtra
);
653
}
654
660
public
function
getLatestStatus
()
661
{
662
return
$this->latestStatus
;
663
}
664
670
public
function
getLatestError
()
671
{
672
return
$this->latestError
;
673
}
674
680
public
function
getError
()
681
{
682
global
$bearsamppLang
;
683
684
if
(!empty($this->latestError)) {
685
return
$bearsamppLang
->getValue(
Lang::ERROR
) .
' '
.
$this->latestError
;
686
} elseif ($this->latestStatus != self::STATUS_NA) {
687
return
$bearsamppLang
->getValue(
Lang::STATUS
) .
' '
. $this->latestStatus .
' : '
. $this->getWin32ServiceStatusDesc($this->latestStatus);
688
}
689
690
return
null
;
691
}
692
}
$result
$result
Definition
ajax.apache.php:19
$bearsamppLang
global $bearsamppLang
Definition
ajax.apache.php:16
$bearsamppRoot
global $bearsamppRoot
Definition
ajax.apache.php:16
$bearsamppCore
global $bearsamppCore
Definition
ajax.latestversion.php:24
Batch\exec
static exec($basename, $content, $timeout=true, $catchOutput=true, $standalone=false, $silent=true, $rebuild=true)
Definition
class.batch.php:453
Lang\ERROR
const ERROR
Definition
class.lang.php:42
Lang\STATUS
const STATUS
Definition
class.lang.php:70
Log\info
static info($data, $file=null)
Definition
class.log.php:737
Log\debug
static debug($data, $file=null)
Definition
class.log.php:726
Log\error
static error($data, $file=null)
Definition
class.log.php:760
Log\initClass
static initClass($classInstance)
Definition
class.log.php:784
Nssm
Definition
class.nssm.php:18
Nssm\setStart
setStart($start)
Definition
class.nssm.php:590
Nssm\INFO_APP_ENVIRONMENT_EXTRA
const INFO_APP_ENVIRONMENT_EXTRA
Definition
class.nssm.php:46
Nssm\SERVICE_DELAYED_START
const SERVICE_DELAYED_START
Definition
class.nssm.php:21
Nssm\$stdout
$stdout
Definition
class.nssm.php:56
Nssm\INFO_APP_STDERR
const INFO_APP_STDERR
Definition
class.nssm.php:44
Nssm\getError
getError()
Definition
class.nssm.php:680
Nssm\getDisplayName
getDisplayName()
Definition
class.nssm.php:520
Nssm\STATUS_PAUSE_PENDING
const STATUS_PAUSE_PENDING
Definition
class.nssm.php:31
Nssm\INFO_APP_DIRECTORY
const INFO_APP_DIRECTORY
Definition
class.nssm.php:41
Nssm\writeLog
writeLog($log)
Definition
class.nssm.php:79
Nssm\setName
setName($name)
Definition
class.nssm.php:510
Nssm\getStdout
getStdout()
Definition
class.nssm.php:600
Nssm\$environmentExtra
$environmentExtra
Definition
class.nssm.php:58
Nssm\INFO_APP_STDOUT
const INFO_APP_STDOUT
Definition
class.nssm.php:45
Nssm\STATUS_RUNNING
const STATUS_RUNNING
Definition
class.nssm.php:33
Nssm\getName
getName()
Definition
class.nssm.php:500
Nssm\STATUS_PAUSED
const STATUS_PAUSED
Definition
class.nssm.php:32
Nssm\setStdout
setStdout($stdout)
Definition
class.nssm.php:610
Nssm\writeLogInfo
writeLogInfo($log)
Definition
class.nssm.php:90
Nssm\create
create()
Definition
class.nssm.php:185
Nssm\__construct
__construct($name)
Definition
class.nssm.php:68
Nssm\$start
$start
Definition
class.nssm.php:55
Nssm\$binPath
$binPath
Definition
class.nssm.php:53
Nssm\$latestStatus
$latestStatus
Definition
class.nssm.php:60
Nssm\writeLogError
writeLogError($log)
Definition
class.nssm.php:101
Nssm\STATUS_START_PENDING
const STATUS_START_PENDING
Definition
class.nssm.php:34
Nssm\exec
exec($args)
Definition
class.nssm.php:114
Nssm\status
status($timeout=true)
Definition
class.nssm.php:148
Nssm\STATUS_CONTINUE_PENDING
const STATUS_CONTINUE_PENDING
Definition
class.nssm.php:30
Nssm\isPaused
isPaused()
Definition
class.nssm.php:435
Nssm\getLatestStatus
getLatestStatus()
Definition
class.nssm.php:660
Nssm\STATUS_NOT_EXIST
const STATUS_NOT_EXIST
Definition
class.nssm.php:37
Nssm\getServiceStatusDesc
getServiceStatusDesc($status)
Definition
class.nssm.php:463
Nssm\INFO_APP_PARAMETERS
const INFO_APP_PARAMETERS
Definition
class.nssm.php:43
Nssm\STATUS_STOPPED
const STATUS_STOPPED
Definition
class.nssm.php:36
Nssm\stop
stop()
Definition
class.nssm.php:313
Nssm\SERVICE_DISABLED
const SERVICE_DISABLED
Definition
class.nssm.php:23
Nssm\getLatestError
getLatestError()
Definition
class.nssm.php:670
Nssm\getStderr
getStderr()
Definition
class.nssm.php:620
Nssm\$latestError
$latestError
Definition
class.nssm.php:59
Nssm\setParams
setParams($params)
Definition
class.nssm.php:570
Nssm\setStderr
setStderr($stderr)
Definition
class.nssm.php:630
Nssm\getStart
getStart()
Definition
class.nssm.php:580
Nssm\infos
infos()
Definition
class.nssm.php:350
Nssm\isStopped
isStopped()
Definition
class.nssm.php:422
Nssm\STATUS_STOP_PENDING
const STATUS_STOP_PENDING
Definition
class.nssm.php:35
Nssm\$name
$name
Definition
class.nssm.php:51
Nssm\getEnvironmentExtra
getEnvironmentExtra()
Definition
class.nssm.php:640
Nssm\$displayName
$displayName
Definition
class.nssm.php:52
Nssm\getBinPath
getBinPath()
Definition
class.nssm.php:540
Nssm\setDisplayName
setDisplayName($displayName)
Definition
class.nssm.php:530
Nssm\restart
restart()
Definition
class.nssm.php:336
Nssm\$stderr
$stderr
Definition
class.nssm.php:57
Nssm\isRunning
isRunning()
Definition
class.nssm.php:409
Nssm\SERVICE_INTERACTIVE_PROCESS
const SERVICE_INTERACTIVE_PROCESS
Definition
class.nssm.php:27
Nssm\setBinPath
setBinPath($binPath)
Definition
class.nssm.php:550
Nssm\getParams
getParams()
Definition
class.nssm.php:560
Nssm\setEnvironmentExtra
setEnvironmentExtra($environmentExtra)
Definition
class.nssm.php:650
Nssm\SERVICE_WIN32_OWN_PROCESS
const SERVICE_WIN32_OWN_PROCESS
Definition
class.nssm.php:26
Nssm\isInstalled
isInstalled()
Definition
class.nssm.php:396
Nssm\SERVICE_DEMAND_START
const SERVICE_DEMAND_START
Definition
class.nssm.php:22
Nssm\INFO_APPLICATION
const INFO_APPLICATION
Definition
class.nssm.php:42
Nssm\SERVICE_AUTO_START
const SERVICE_AUTO_START
Definition
class.nssm.php:20
Nssm\start
start()
Definition
class.nssm.php:290
Nssm\PENDING_TIMEOUT
const PENDING_TIMEOUT
Definition
class.nssm.php:48
Nssm\STATUS_NA
const STATUS_NA
Definition
class.nssm.php:38
Nssm\isPending
isPending($status)
Definition
class.nssm.php:450
Nssm\$params
$params
Definition
class.nssm.php:54
Nssm\SLEEP_TIME
const SLEEP_TIME
Definition
class.nssm.php:49
Path\formatWindowsPath
static formatWindowsPath($path)
Definition
class.path.php:127
Path\getNssmLogFilePath
static getNssmLogFilePath($aetrayPath=false)
Definition
class.path.php:770
Path\getNssmExe
static getNssmExe($aetrayPath=false)
Definition
class.path.php:691
Registry\HKEY_LOCAL_MACHINE
const HKEY_LOCAL_MACHINE
Definition
class.registry.php:23
Win32Native\getServiceInfo
static getServiceInfo($serviceName, $properties=[])
Definition
class.win32native.php:951
Win32Service\VBS_PATH_NAME
const VBS_PATH_NAME
Definition
class.win32service.php:74
$command
if( $raw) foreach($opts as $opt) if( $store_system_config) if($store_user_config) $command
Definition
pearcmd.php:322
sandbox
core
classes
class.nssm.php
Generated by
1.17.0