proc-repeat is automated shell script executor by node.js.
$ npm install --save proc-repeat
Example of run command "$ node -v" every 500ms only 5 times and write the result into current path named schedule1.
const repeat = require('./index.js');
repeat('node -v').every(500, 'ms').only(5)
.config({
writeFile: true,
writePath: __dirname,
name: 'schedule1',
})
.begin();MIT. Free to use.
- Not much, just some bug fixes.
- Added more methods for easy and intuitive code.
- Now writing file format also supports text type.
- You can now seperate stdout and stderr with seperate config option.
- You can now select which output(stdout, stderr) want to "not included" when writing file.
- Some bug fixes.
- You don't have to specify writePath when writeFile is true. Default path is where you executed.
- You can define schedule as JSON and can load it from JavaScript. This also will use for CLI command to execute schedules. (Not current version)
- Every path like parameters are now based on where you executed.
- You can set append mode with appendMode option. Append Mode is only works with text mode, not json mode.
- Added unixTimeStamp option. If this false, using time string like 2016-11-17 16:34:30:99 when logging. Default is true.
proc-repeat is designed for supports two options to handling the process: CPS and Event Handlers. begin() method which start the action accept the first parameter as function, callback, which call after shell command is done.
repeat('pwd').begin((err, stdout, stderr) => {
if(err) {
return console.error(err);
}
console.log('stdout', stdout);
});repeat() is a factory function that internally create new object and return. The Object returned from method implemented EventEmitter, so you can attach the event handlers. Possible events are:
- error: Fires when it has error
- done: Fires when single command execution is over.
- end: Fires when stopped everthing(I will explain this later).
repeat('pwd')
.on('error', (err) => {
console.error(err);
})
.on('done', (stdout, stderr) => {
console.log('stdout', stdout)
})
.on('end', () => {
console.log('All done!');
});You can also define your schedule not just in JavaScript, with JSON. Each key is the name of a method of proc-repeat, like from, to, until, every and so on. For create schedule as JSON, create [name].json first.
{
"cmd": "pwd",
"every": [300, "ms"],
"config": {
"writeFile": true,
"writePath": "./",
"name": "schedule1"
}
}And then create JavaScript for execute this. For use JSON, you can use repeat.load():
const repeat = require('proc-repeat');
repeat.load('./schedule1.json').begin();You can also define multiple schedules as JSON and load them entirely at once.
let schedules = repeat.load(['./schedule1.json', './schedule2.json']);
schedules[0].begin();
schedules[1].begin();Not hard, isn't it? Only you should be careful is the setting path, when writing file or change run path before do command with runFrom(), the source path that module finds something with your customized path is where you executed this JavaScript file. That means that if you create JS file that load JSON and execute from current directory, all your directory settings are based on current location.
You can possible to create reserved or make time limit with from() method and to() method. Both method has single argument, date or string. Internally it uses moment.js, so you can easily handle this if you are already know about it.
// run "$ pwd" after 2016-10-14 18:00:00
repeat('pwd').from('2016-10-14 18:00:00')
.begin();// run until 2016-10-14 19:00:00
repeat('pwd').to('2016-10-14 19:00:00')
.begin();Or you can use until method, alias of to method.
repeat('pwd').until('2016-10-14 19:00:00')
.begin();You can combine them.
repeat('pwd').from('2016-10-14 18:00:00').to('2016-10-14 19:00:00')
.begin();Most of the time, you want to write the result to a file. You can use config option:
repeat('pwd')
.config({
writeFile: true,
writePath: __dirname
})
.begin();You can set the name of a file with name option or use as() method.
repeat('pwd')
.config({
writeFile: true,
writePath: __dirname
name: 'myschedule1'
});or
repeat('pwd').config({ writeFile: true, writePath: __dirname }).as('myschedule1').begin();Both are totally same. For write file, you must set writePath too, otherwise it fails.
** Since 1.0.4, you don't have to specify writePath. By default, it writes where you executed. Only use this property when you want to set to different location.
Create new schedule. Basic settings(default) are run the command every 1 second until you manually stop it(with stop method).
const repeat = require('proc-repeat');
repeat('ls -al').begin();Load JSON based schedule(s). It works synchronously. If you pass single string, it will return new proc-repeat type object. But if you pass array of strings, it will return the array that has each proc-repeat object. Becareful when loading multiple JSONs and execute them.
Set the time when it starts.
repeat('ls -al').from('2016-10-14 18:00:00').begin();Set the time limit.
repeat('ls -al').until('2016-10-14 18:00:00').begin();Set the duration. timeUnit is string that must one of these values:
- week
- weeks
- w
- days
- day
- d
- hour
- hours
- h
- minute
- minutes
- m
- seconds
- second
- s
- msecond
- mseconds
- millisecond
- milliseconds
- ms
repeat('ls -al').every(10, 'seconds').begin();Set the iteration count.
repeat('ls -al').only(2).begin();Execute schedule only once. Same as times(1).
repeat('ls -al').once().begin();Name the schedule.
repeat('ls -al').config({ writeFile: true, writePath: __dirname }).as('schedule1').begin();Delay the start of the execution. With "from", also delays when it starts execution.
repeat('ls -al').after(5, 'seconds').begin();repeat('ls -al').from('2016-10-14 18:00:00').after(1, 'minute').begin();Write output as a file.
repeat('ls -al').saveAsFileTo(__dirname);Set format to write as a file. Valid values are 'json' and 'text'. Default is 'json'.
repeat('ls -al').saveAsFileTo(__dirname).asFormat('text')Set write format to json. Most of time, you don't have to call this because JSON is default option.
Set write format to text.
Set run path.
repeat('pwd').runFrom('/home/ec2-user').saveAsFile(__dirname);Set the options.
- bool writeFile: Write file. Default is false. You can now change format from 1.0.3
- bool writePath: Set the path where to write a file. Default is current directory.
- bool writeFormat: (New 1.0.3) Set the format which want to print. Default is 'json'. Valid values are 'text' and 'json'(until yet).
- bool seperate: (New 1.0.3) Divide stdout and stderr into seperate files. Default is false.
- bool stdout: (New 1.0.3) Set true to write stdout into a file. Default is true.
- bool stderr: (New 1.0.3) Set true to write stderr into a file. Default is true.
- string name: Set the name of this schedule. Same as using as() method.
- string runPath: Path for executing the command. Default is current directory.
Start the schedule.
repeat('ls -al').begin();Finish the schedule. It will fires "end" event also.
let schedule = repeat('ls -al').begin();
setTimeout(() => {
schedule.stop();
}, 3000);proc-repeat provides lots of methods that easily combime them. Only you need is a little bit of creativity.
repeat('pwd').every(1, 'second').only(10).until('2016-10-16 18:00:00')
.begin();repeat('node -v').every(300, 'ms').from('2016-10-16 18:00:00').to('2016-10-16 18:01:00');You can set append mode from v1.0.5, for writing multiple logs at single file. Only each first execution will make a file.
let schedule = repeat('pwd').times(20).every(300, 'ms')
.config({
runPath: __dirname,
writeFile: true,
unixTimeStamp: false,
appendMode: true,
writeFormat: 'text', // make sure to set writeFormat as 'text', 'json' will throw exception.
seperate: true
});- CLI execution with specific setting files like:
schedules.json
{
"schedule1": {
"cmd": "ls -al",
"from": "2014-10-24 11:24:30",
"to": "2014-10-25 11:24:30",
"every": [5, "minutes"],
"config": {
"writeFile": true,
"name": "result",
"writePath": "/home/ec2-user/app",
"stderr": false
}
}
}$ proc-repeat schedules.json
1.0.4 version can be little buggy, so please notice me on github page if you faced some problems. Thanks!