{"id":22602788,"url":"https://github.com/wahengchang/nodejs-cron-job-must-know","last_synced_at":"2025-04-11T03:24:08.561Z","repository":{"id":84319825,"uuid":"100903996","full_name":"wahengchang/nodejs-cron-job-must-know","owner":"wahengchang","description":"it is an example of running node.js script with every certain period(cron job)","archived":false,"fork":false,"pushed_at":"2018-10-31T04:56:30.000Z","size":7,"stargazers_count":40,"open_issues_count":0,"forks_count":15,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-22T01:38:16.711Z","etag":null,"topics":["cron","cron-jobs","cronjob","example","javascript","nodejs","reactjs","schedule","scheduler","tutorial"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wahengchang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-08-21T02:25:18.000Z","updated_at":"2024-12-29T17:53:37.000Z","dependencies_parsed_at":"2023-10-19T00:16:28.757Z","dependency_job_id":null,"html_url":"https://github.com/wahengchang/nodejs-cron-job-must-know","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wahengchang%2Fnodejs-cron-job-must-know","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wahengchang%2Fnodejs-cron-job-must-know/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wahengchang%2Fnodejs-cron-job-must-know/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wahengchang%2Fnodejs-cron-job-must-know/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wahengchang","download_url":"https://codeload.github.com/wahengchang/nodejs-cron-job-must-know/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248334557,"owners_count":21086416,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cron","cron-jobs","cronjob","example","javascript","nodejs","reactjs","schedule","scheduler","tutorial"],"created_at":"2024-12-08T12:42:29.815Z","updated_at":"2025-04-11T03:24:08.551Z","avatar_url":"https://github.com/wahengchang.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nodejs-cron-job-must-know\nit is an example of running Node.js script with every certain period(cron and non-cron job)\n\n#### Why don't we use Linux `crontab` \n - We can provide the full path to node `/usr/local/bin/node` in your cron job like:\n    - `30 6 1 * * /usr/local/bin/node /home/steve/example/script.js`\n - Or making a script with the command, and then adding that to cron:\n    ```\n    #!/usr/bin/env sh \n    node /home/campaigns/reporting/UNIT_TESTS/testCron.js \u003e /home/campaigns/reporting/UNIT_TESTS/cron.log\n    ```\n- The problem with the two above methods is messing up the path. The command is in `absolute path`, but the Node.js script uses `relative path` to import/require other modules. It causes the error of file not found. __*So we need to execute the cron under the directory of Node.js script, which contains all the modules which will be used*__.\n\n\n\n## Install \nThis lib is used to keep the cron-job alive, which triggers the node script at a certain time.\n\n```\n$ npm install --save node-cron\n```\n\n## Usage \nImport node-cron and schedule a task:\n\n[Read more](https://www.npmjs.com/package/node-cron)\n\n```js\nvar cron = require('node-cron');\n \ncron.schedule('* * * * *', function(){\n  console.log('running a task every minute');\n});\n\n```\n\n## Run Node.js script in cron\n\n - To run script : `$ node script1.js` \n - And script: `$ npm run script -- PeterGood`\n - `child_help.js` is an amazing Node.js script from [mout](https://github.com/mout/mout/), which helps to manage multiple linux commands. \n\n\nStart a Daemon, and run \n```\n$ node cronNodeScript\n```\n\nExecute script every 1 min\n```js\n//execute every 1 min\ncron.schedule('*/1 * * * *', function(){\n    ....\n});\n```\n\nExecute `$ node script1.js` and `npm run script -- PeterGood` every 1 min\n```js\n\n//execute every 1 min\ncron.schedule('*/1 * * * *', function(){\n    var shell = require('./child_helper');\n\n    var commandList = [\n        \"node script1.js\",\n        \"npm run script -- PeterGood\"\n    ]\n\n    shell.series(commandList , function(err){\n    //    console.log('executed many commands in a row'); \n        console.log('done')\n    });\n});\n```\n\n\n\n\n\n## Reference:\n - [https://www.npmjs.com/package/node-schedule](https://www.npmjs.com/package/node-schedule)\n - [https://www.npmjs.com/package/node-cron](https://www.npmjs.com/package/node-cron)\n - [https://stackoverflow.com/questions/7194102/node-js-script-not-executing-from-crontab](https://stackoverflow.com/questions/7194102/node-js-script-not-executing-from-crontab)\n - [https://stackoverflow.com/questions/5849402/how-can-you-execute-a-node-js-script-via-a-cron-job](https://stackoverflow.com/questions/5849402/how-can-you-execute-a-node-js-script-via-a-cron-job)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwahengchang%2Fnodejs-cron-job-must-know","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwahengchang%2Fnodejs-cron-job-must-know","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwahengchang%2Fnodejs-cron-job-must-know/lists"}