-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdaemon.js
More file actions
89 lines (77 loc) · 2.09 KB
/
daemon.js
File metadata and controls
89 lines (77 loc) · 2.09 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* Created by sbanas on 15.04.2017.
*/
'use strict';
const _ = require('lodash');
const yamlConfig = require('yaml-config');
const config = yamlConfig.readConfig('./config.yml');
const defaultConfig = {
main: "server.js",
name: "server",
pidfile: "server.pid",
silent: false
};
//noinspection JSUnresolvedVariable
const daemon = require("daemonize2")
.setup(_.extend({}, defaultConfig, config.daemon));
if (process.getuid && process.getuid() !== 0) {
console.log("Expected to run as root");
process.exit(1);
}
daemon
.on("starting", function() {
console.log("Starting daemon...");
})
.on("started", function(pid) {
console.log("Daemon started. PID: " + pid);
})
.on("stopping", function() {
console.log("Stopping daemon...");
})
.on("stopped", function() {
console.log("Daemon stopped.");
})
.on("running", function(pid) {
console.log("Daemon already running. PID: " + pid);
})
.on("notrunning", function() {
console.log("Daemon is not running");
})
.on("error", function(err) {
console.log("Daemon failed to start: " + err.message);
});
switch (process.argv[2]) {
case "start":
daemon.start().once("started", function() {
process.exit();
});
break;
case "stop":
daemon.stop();
break;
case "kill":
daemon.kill();
break;
case "restart":
if (daemon.status()) {
daemon.stop().once("stopped", function() {
daemon.start().once("started", function() {
process.exit();
});
});
} else {
daemon.start().once("started", function() {
process.exit();
});
}
break;
case "status":
const pid = daemon.status();
if (pid)
console.log("Daemon running. PID: " + pid);
else
console.log("Daemon is not running.");
break;
default:
console.log("Usage: [start|stop|kill|restart|status]");
}