-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli-repeat.js
executable file
·76 lines (68 loc) · 2.26 KB
/
cli-repeat.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
71
72
73
74
75
76
#!/opt/local/bin/node
var program = require('commander'),
proc = require('child_process');
program
.version('0.0.1')
.option('-c --command [command]', 'command to run')
.option('-i --interval [interval]', 'execute command every x seconds (can be fraction). defaults to 60 seconds', Number, 60)
.option('-t --timeout [timeout]', 'stop after x seconds. default [0] will run forever', Number, 0)
.option('-d --delay [timeout]', 'number of seconds before executing the command for first time. default [0] will run command immediately', Number, 0)
.option('-v --verbose', 'output debug info and not just command output')
.parse(process.argv);
if(!process.argv[2]){
console.log(program.helpInformation());
}else{
start();
}
function start(){
if(program.interval <= 0){
log('--internval must be greater than 0');
return;
}
if(program.delay > program.timeout){
log('--timeout must be greater than --delay, otherwise your command will never run');
return;
}
if(!program.command){
log('--command not specified');
return;
}
var self = this;
var interval = program.interval * 1000,
delay = program.delay * 1000;
this.i = 1;
this.timeout = new Date((new Date()).getTime() + program.timeout*1000);
if(program.delay && program.verbose){
log('Delaying start for '+ program.delay + ' seconds');
}
this._setTimeout = setTimeout(function(){
exec(checkTimeout);
self._setInterval = setInterval(function(){
if(program.verbose){
log('---- REPEATING '+ self.i++ +'----');
}
exec(checkTimeout);
}, interval);
}, delay);
}
function checkTimeout(){
if(program.timeout>0 && (new Date((new Date()).getTime() + program.interval*1000)) > timeout){
clearInterval(this._setTimeout);
clearTimeout(this._setInterval);
return;
}
}
function exec(callback){
proc.exec(program.command, function(error, stdout, stderr){
log(stdout);
if(error || stderr){
log('----ERROR REPEATING----');
log(error);
log(stderr);
}
callback();
});
}
function log(message){
console.log((new Date()) + ' ' + message);
}