setInterval for the command line, with an option to stop after N repetitions.
setInterval for the command line.
npm install -g shell-interval
$ shell-interval -t 5 -c "echo Hello." -r 3Hello. # 0:05Hello. # 0:10Hello. # 0:15# [stop]
var shellInterval = require"shell-interval";shellIntervaloptions:command: "echo ten seconds has passed"time: 10reps: 5if err throw err;console.logstdout;console.log"The shell command was called five times. Exiting...";;
shell-interval -c "[command]" -t 2 : Execute the
[command] every ~ two seconds indefinitely, until a kill signal is received. (e.g.
shell-interval -c "[command]" -t 5 -r 10 : Execute the
[command] every ~five seconds until the command has been called ten times, and then exit.
Command is first invoked ~five seconds after the call to
shell-interval -c "[command]" -t 5 -r 10 -e : Execute the
[command] every ~five seconds until the command has been called ten times, and then exit. Because the
--eager) flag was passed, the command is first executed as soon as node finishes initializing the script.
shell-interval module exports a single function with the same options as its command-line interface as well as hooks for two callbacks. These callbacks can handle data returned from the shell calls (
onExec) and perform any necessary cleanup after a finite series of shell calls has completed (
var shellInterval = require"shell-interval";var timeout_id = shellIntervaloptions: /* object */onExec: /* callback function */onFinish: /* callback function */;
* __`options` [object] :__ a hash of values corresponding to set-interval's CLI flags. __Required.__ * `command` __[string] :__ the shell command to execute. __Required.__ * `time` __[number] :__ the number of seconds between calls. __Required.__ * `reps` __[number] :__ a number of total shell calls to make. After this number is reached, the interval will be cleared. _Optional._ * `eager` __[boolean] :__ whether to immediately execute the function once before starting the timer. _Optional._ * `quiet` __[boolean] :__ when used with the default `onExec` handler, tells set-interval not to print `stdout` to the terminal. _Optional._ * `onExec` __[function] :__ a callback to handle data returned from shell command execution. It is invoked once for each shell call and receives `err`, `stdout`, and `stderr` as its arguments. _Optional._ * `onFinish` __[function] :__ when a limited number of repetitions is specified(in `options.reps`), this callback is invoked after the last shell call has returned a value and been handled. _Optional. Only has an effect if `reps` is specified._
timeoutIdassigned to shell-interval's internal timer. This can be used with
clearInterval(<id>)to stop executing the shell command.
Both callbacks are optional, but the hash of parameters passed in must contain an
options itself must be a hash containing at least
shellIntervaloptions:command: "echo Hello."time: 5;// echoes "Hello" in the terminal every five seconds// until a break signal is received.
More usefully, client code can supply two optional callback functions: one for each of the
onExecis invoked once for every execution of the shell command, and receives
stderras its arguments.
onFinishis only called if a finite number of repetitions has been specified (via
options.repsin node or
-r <number>in the shell). In this case it is called exactly once, after the last callback has returned its value and been handled. It does not receive any arguments.
shellIntervaloptions:command: "echo Hello."time: 2reps: 3if err throw err;console.log"Output: " + stdout;console.log"Finished.";
onExec handler logs the shell response to the terminal unless the
--quiet flag is passed. Because this default handler is overridden by passing in a callback, the
--quiet flag will not affect the behavior of a client-provided function.
####Usage in Place of
The function exported by
shell-interval returns the Node
timeoutId set by shell-interval internally, it can be used as a transparent replacement for a
setInterval call in many cases where periodic shell execution is needed. Most importantly, the return value can be passed to the normal
clearInterval function to stop executing the shell command.
var shellInterval = require"shell-interval";var intervalId = shellIntervaloptions:command: "echo Hello."time: 5if err throw err;console.logstdout;if /* some condition */clearIntervalintervalId;;
$ shell-interval -c "make" -t 10make: nothing to be done
npm install -g shell-interval
##Bugs and Issues Use the GitHub issue tracker.
##Development See the GitHub page.
Pull requests are welcome. Include Mocha-compatible test specs and documentation of any changes to the API.
The MIT License (MIT)
Copyright (c) 2013 Scott Ivey, firstname.lastname@example.org
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.