-
Notifications
You must be signed in to change notification settings - Fork 374
/
linux.service.ts
57 lines (46 loc) · 1.44 KB
/
linux.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import * as child_process from 'child_process';
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '../../../core/config/config.service';
@Injectable()
export class LinuxService {
constructor(
private readonly configService: ConfigService,
private readonly logger: Logger,
) { }
/**
* Reboot the host
*/
restartHost() {
const cmd = [(this.configService.ui.linux && this.configService.ui.linux.restart) ? this.configService.ui.linux.restart : 'shutdown -r now'];
if (this.configService.ui.sudo) {
cmd.unshift('sudo -n');
}
this.logger.warn(`Rebooting linux server with command: "${cmd.join(' ')}"`);
setTimeout(() => {
child_process.exec(cmd.join(' '), (err) => {
if (err) {
this.logger.error(err.message);
}
});
}, 100);
return { ok: true, command: cmd };
}
/**
* Shutdown the host
*/
shutdownHost() {
const cmd = [(this.configService.ui.linux && this.configService.ui.linux.shutdown) ? this.configService.ui.linux.restart : 'shutdown -h now'];
if (this.configService.ui.sudo) {
cmd.unshift('sudo -n');
}
this.logger.warn(`Shutting down linux server with command: "${cmd.join(' ')}"`);
setTimeout(() => {
child_process.exec(cmd.join(' '), (err) => {
if (err) {
this.logger.error(err.message);
}
});
}, 500);
return { ok: true, command: cmd };
}
}