1
1
import { spawn } from 'node:child_process' ;
2
2
import os from 'node:os' ;
3
- import { getRootDir } from '@iobroker/js-controller-common-db/tools' ;
3
+ import { execAsync , getRootDir } from '@iobroker/js-controller-common-db/tools' ;
4
4
import path from 'node:path' ;
5
5
import url from 'node:url' ;
6
6
@@ -9,25 +9,45 @@ import url from 'node:url';
9
9
*
10
10
* @param callback callback to execute after restart is triggered
11
11
*/
12
- export default function restart ( callback ?: ( ) => void ) : void {
12
+ export default async function restart ( callback ?: ( ) => void ) : Promise < void > {
13
13
let cmd ;
14
14
let args ;
15
15
if ( os . platform ( ) === 'win32' ) {
16
- // On Windows, we execute the controller entry point directly
17
- cmd = path . join ( getRootDir ( ) , 'iob.bat' ) ;
18
- args = [ 'restart' ] ;
16
+ // On Windows, we use powershell to restart the service, because execution of bat files is no more possible
17
+ const envPath = path . join ( getRootDir ( ) , '.env' ) . replaceAll ( '\\' , '\\\\' ) ;
18
+ cmd = `powershell -Command "$envPath = \\"${ envPath } \\";
19
+ $iobServiceName = \\"ioBroker\\";
20
+ if (Test-Path $envPath) {
21
+ foreach ($line in Get-Content $envPath) {
22
+ $line = $line.Trim();
23
+ if ($line -match \\"^\\s*iobservicename\\s*=\\s*(.+)\\s*$\\") {
24
+ $iobServiceName = $matches[1].Trim(); break;
25
+ }
26
+ }
27
+ }
28
+ Write-Output \\"Restarting service $iobServiceName.exe\\";Restart-Service \\"$iobServiceName.exe\\" -Force"` ;
29
+
30
+ // Remove line breaks, because the powershell command will fail otherwise
31
+ cmd = cmd . replace ( / [ \r \n ] + / gm, ' ' ) ;
32
+
33
+ try {
34
+ await execAsync ( cmd ) ;
35
+ } catch ( e ) {
36
+ console . error ( `Restart failed: ${ e . message } ` ) ;
37
+ }
19
38
} else {
20
39
// Unix has a global ioBroker binary that delegates to the init system
21
40
// We need to call that, so we don't have two instances of ioBroker running
22
41
cmd = 'iobroker' ;
23
42
args = [ 'restart' ] ;
43
+
44
+ const child = spawn ( cmd , args , {
45
+ detached : true ,
46
+ stdio : [ 'ignore' , 'ignore' , 'ignore' ] ,
47
+ windowsHide : true
48
+ } ) ;
49
+ child . unref ( ) ;
24
50
}
25
- const child = spawn ( cmd , args , {
26
- detached : true ,
27
- stdio : [ 'ignore' , 'ignore' , 'ignore' ] ,
28
- windowsHide : true
29
- } ) ;
30
- child . unref ( ) ;
31
51
if ( typeof callback === 'function' ) {
32
52
setTimeout ( ( ) => callback ( ) , 500 ) ;
33
53
} else {
0 commit comments