├── package.json ├── croncli ├── README.md ├── LICENSE └── .gitignore /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-cron-cli", 3 | "version": "1.0.1", 4 | "description": "Command Line Implementation for Node Cron Under 20 Lines.", 5 | "main": "croncli", 6 | "bin": { 7 | "croncli": "./croncli" 8 | }, 9 | "dependencies": { 10 | "commander": "^6.2.1", 11 | "cron": "^1.8.2" 12 | }, 13 | "devDependencies": {}, 14 | "scripts": { 15 | "test": "echo \"Error: no test specified\" && exit 1" 16 | }, 17 | "homepage": "https://github.com/yuis-ice/node-cron-cli", 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/yuis-ice/node-cron-cli.git" 21 | }, 22 | "keywords": [ 23 | "nodejs", 24 | "cli", 25 | "cron", 26 | "command-line", 27 | "cli-app", 28 | "command-line-tool" 29 | ], 30 | "author": "Fumiya Arisaka ", 31 | "license": "BSD-3-Clause" 32 | } 33 | -------------------------------------------------------------------------------- /croncli: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const exec = require('child_process').exec; 4 | const CronJob = require('cron').CronJob; 5 | const commander = require('commander'); 6 | const program = new commander.Command(); 7 | 8 | program 9 | .option('-p, --pattern ', 'cron pattern ', "*/5 * * * * *" ) 10 | .option('-t, --timezone ', 'timezone ', 'Asia/Tokyo' ) 11 | .option('-e, --execute ', 'command to be executed ', "echo Hi." ) 12 | .option('-l, --log', 'enable console log ') 13 | .parse(process.argv) 14 | ; if (! process.argv.slice(2).length) program.help() ; 15 | 16 | (new CronJob(program.pattern, async function() { 17 | if (program.log) console.log(`[${program.pattern}] Job fired:`, new Date().toLocaleTimeString("en-US", {timeZone: program.timezone, year: "numeric", month: '2-digit', day: '2-digit', hour: '2-digit', minute:'2-digit', second:'2-digit', timeZoneName: 'short'}) ); 18 | await exec(program.execute, (error, stdout, stderr) => { if ( error ){ console.log(error) } if ( stdout ){ console.log(stdout) } if ( stderr ){ console.log(stderr) } }); 19 | })).start() ; 20 | 21 | if (program.log) console.log("Jobs started...") ; 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Node Cron CLI 3 | 4 | Command Line Implementation for Node Cron Under 20 Lines. 5 | 6 | ## Quick Start 7 | 8 | This will execute your specified command `pwd` each 2 seconds. 9 | 10 | ``` 11 | $ croncli -p "*/2 * * * * *" -e "pwd" --log 12 | ``` 13 | 14 | The output be like: 15 | 16 | ``` 17 | Jobs started... 18 | [*/2 * * * * *] Job fired: 05/12/2021, 08:42:28 PM GMT+9 19 | /home/ubuntu 20 | [*/2 * * * * *] Job fired: 05/12/2021, 08:42:30 PM GMT+9 21 | /home/ubuntu 22 | [*/2 * * * * *] Job fired: 05/12/2021, 08:42:32 PM GMT+9 23 | /home/ubuntu 24 | ``` 25 | 26 | ## Installation 27 | 28 | ``` 29 | npm i --global node-cron-cli 30 | ``` 31 | 32 | ## Examples 33 | 34 | Every hour 35 | ``` 36 | 0 0 * * * * 37 | ``` 38 | 39 | Every day 40 | ``` 41 | 0 0 0 * * * 42 | ``` 43 | 44 | Every 4 hours 45 | ``` 46 | 0 0 */4 * * * 47 | ``` 48 | 49 | Every day at 11am and 11pm 50 | ``` 51 | 0 0 11,23 * * * 52 | ``` 53 | 54 | For more info, see [kelektiv/node-cron](https://github.com/kelektiv/node-cron) 55 | 56 | ## Command line options 57 | 58 | ``` 59 | $ ./cron-cli.js 60 | Usage: cron-cli [options] 61 | 62 | Options: 63 | -p, --pattern cron pattern (default: "*/5 * * * * *") 64 | -t, --timezone timezone (default: "Asia/Tokyo") 65 | -e, --execute command to be executed (default: "echo Hi.") 66 | -l, --log enable console log 67 | -h, --help display help for command 68 | ``` 69 | 70 | ## LICENSE 71 | 72 | This software is released under the BSD-3-Clause license. 73 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2021, Fumiya Arisaka 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | *.vim.sh 3 | 4 | *.db 5 | .*.js 6 | .*.md 7 | *.tmp 8 | 9 | # [gitignore/Node.gitignore at master · github/gitignore](https://github.com/github/gitignore/blob/master/Node.gitignore) 10 | 11 | # Logs 12 | logs 13 | *.log 14 | npm-debug.log* 15 | yarn-debug.log* 16 | yarn-error.log* 17 | lerna-debug.log* 18 | 19 | # Diagnostic reports (https://nodejs.org/api/report.html) 20 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 21 | 22 | # Runtime data 23 | pids 24 | *.pid 25 | *.seed 26 | *.pid.lock 27 | 28 | # Directory for instrumented libs generated by jscoverage/JSCover 29 | lib-cov 30 | 31 | # Coverage directory used by tools like istanbul 32 | coverage 33 | *.lcov 34 | 35 | # nyc test coverage 36 | .nyc_output 37 | 38 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 39 | .grunt 40 | 41 | # Bower dependency directory (https://bower.io/) 42 | bower_components 43 | 44 | # node-waf configuration 45 | .lock-wscript 46 | 47 | # Compiled binary addons (https://nodejs.org/api/addons.html) 48 | build/Release 49 | 50 | # Dependency directories 51 | node_modules/ 52 | jspm_packages/ 53 | 54 | # Snowpack dependency directory (https://snowpack.dev/) 55 | web_modules/ 56 | 57 | # TypeScript cache 58 | *.tsbuildinfo 59 | 60 | # Optional npm cache directory 61 | .npm 62 | 63 | # Optional eslint cache 64 | .eslintcache 65 | 66 | # Microbundle cache 67 | .rpt2_cache/ 68 | .rts2_cache_cjs/ 69 | .rts2_cache_es/ 70 | .rts2_cache_umd/ 71 | 72 | # Optional REPL history 73 | .node_repl_history 74 | 75 | # Output of 'npm pack' 76 | *.tgz 77 | 78 | # Yarn Integrity file 79 | .yarn-integrity 80 | 81 | # dotenv environment variables file 82 | .env 83 | .env.test 84 | 85 | # parcel-bundler cache (https://parceljs.org/) 86 | .cache 87 | .parcel-cache 88 | 89 | # Next.js build output 90 | .next 91 | out 92 | 93 | # Nuxt.js build / generate output 94 | .nuxt 95 | dist 96 | 97 | # Gatsby files 98 | .cache/ 99 | # Comment in the public line in if your project uses Gatsby and not Next.js 100 | # https://nextjs.org/blog/next-9-1#public-directory-support 101 | # public 102 | 103 | # vuepress build output 104 | .vuepress/dist 105 | 106 | # Serverless directories 107 | .serverless/ 108 | 109 | # FuseBox cache 110 | .fusebox/ 111 | 112 | # DynamoDB Local files 113 | .dynamodb/ 114 | 115 | # TernJS port file 116 | .tern-port 117 | 118 | # Stores VSCode versions used for testing VSCode extensions 119 | .vscode-test 120 | 121 | # yarn v2 122 | .yarn/cache 123 | .yarn/unplugged 124 | .yarn/build-state.yml 125 | .yarn/install-state.gz 126 | .pnp.* 127 | --------------------------------------------------------------------------------