-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·70 lines (59 loc) · 1.45 KB
/
index.js
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
'use strict'
var child = require('child_process')
, name = require('path').basename(process.argv[1])
, type = process.argv[2]
child.exec("pgrep -fl 'node "+__dirname+"/daemon'", function(err, old)
{
//Remove \n at end of old environment
old = old && old.slice(0, -1).split(' ')
if ('stop' == type || 'restart' == type)
{
if (old)
{
var pid = old.shift()
child.exec("kill "+pid)
console.log("stopped %s on pid %s", name, pid)
}
else
{
console.log("%s is not running", name)
}
if ('restart' == type)
{
type = old[2]
}
}
if ( ! type)
{
return console.log('%s needs <env> set', name)
}
if ('stop' != type)
{
if (old.length != 4)
{
//Spawn lets stdin - but not stdout or stderr - work with detached
var daemon = child.spawn("node", [__dirname+"/daemon", type],
{
detached:true, stdio:[0, 'ignore', 'ignore', 'ipc']
})
//We use ipc channel to replace stdout and stderr since values
//'pipe' or 'inherit' will cause child to hang up with daemon
daemon.on('message', function(data)
{
process[data.type].write(data.write)
})
//We can close this process and leave the daemon running
//give user instructions on how to stop the daemon
process.on('SIGINT', function()
{
console.log("\n\n%s running on pid %s", name, daemon.pid)
console.log("'node %s stop' to quit\n\n", name)
process.exit()
})
}
else
{
console.log('%s is already running', name)
}
}
})