├── README.md ├── bin.js ├── index.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | run-every 2 | === 3 | 4 | ## Install 5 | 6 | ```bash 7 | npm install -g run-every 8 | ``` 9 | 10 | ## Usage 11 | 12 | ```bash 13 | run-every 10 git pull # will do a git pull every 10 seconds 14 | ``` 15 | 16 | -------------------------------------------------------------------------------- /bin.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | if (process.argv.length < 4) { 4 | console.error('usage: run-every command ...'); 5 | process.exit(1); 6 | } 7 | 8 | var runEvery = require('./'); 9 | 10 | var repeatWaitSeconds = process.argv[2]; 11 | var command = process.argv.slice(3); 12 | runEvery(repeatWaitSeconds, command, process); 13 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var spawn = require('child_process').spawn; 2 | 3 | module.exports = runEvery; 4 | 5 | function runEvery(repeatWaitSeconds, command, process) { 6 | 7 | var proc; 8 | var wasKilled = false; 9 | var timeout; 10 | var didSetupProcess = false; 11 | 12 | inner(); 13 | 14 | function inner() { 15 | var start = Date.now(); 16 | proc = spawn(command[0], command.slice(1), { stdio: 'inherit' }); 17 | proc.on('exit', function (code, signal) { 18 | var finished = Date.now(); 19 | if (code === 0 && signal === null) { 20 | var took = finished - start; 21 | var next = repeatWaitSeconds * 1000 - took; 22 | if (next <= 0) next = 0; 23 | timeout = setTimeout(inner, next); 24 | } else { 25 | process.kill(process.pid, signal); 26 | } 27 | 28 | }); 29 | 30 | if (!didSetupProcess) { 31 | process.on('SIGINT', function () { 32 | clearTimeout(timeout); 33 | proc.kill('SIGINT'); 34 | proc.kill('SIGTERM'); 35 | }); 36 | didSetupProcess = true; 37 | } 38 | } 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "run-every", 3 | "version": "0.0.1", 4 | "description": "run a command over and over", 5 | "main": "index.js", 6 | "bin": { 7 | "run-every": "./bin.js" 8 | }, 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/kolodny/run-every.git" 15 | }, 16 | "keywords": [ 17 | "cli", 18 | "watch", 19 | "every", 20 | "script" 21 | ], 22 | "author": "Moshe Kolodny", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/kolodny/run-every/issues" 26 | }, 27 | "homepage": "https://github.com/kolodny/run-every#readme" 28 | } 29 | --------------------------------------------------------------------------------