In this sample we are logging a text every second to the console, the filesystem and to the system d status log.
Prerequisits
Installed node.js, npmCreate your project
create a folder "/var/www/html/sheduler"Init a npm project
create a package.json with following content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "sheduler", | |
"version": "1.0.0", | |
"description": "a simple sheduler", | |
"main": "index.js", | |
"dependencies": { | |
"chai": "^3.5.0", | |
"mocha": "^3.0.2", | |
"node-cron": "^1.1.1", | |
"winston": "^2.2.0" | |
}, | |
"devDependencies": {}, | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [ | |
"cron", | |
"node" | |
], | |
"author": "pboethig", | |
"license": "MIT", | |
"bugs": { | |
"url": "https://github.com/pboethig/node-cron/issues" | |
}, | |
"homepage": "https://github.com/pboethig/node-cron#readme" | |
} |
$ npm install
Install the sheduler
We want to use node-cron for our sheduler. Node-cron allows us to trigger a script every second. That is ideal, if you want to build a worker wich runs every second. install follwing script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var winston = require('winston'); | |
var logger = new (winston.Logger)({ | |
transports:[ | |
new winston.transports.File({ | |
level: 'info', | |
filename: '/var/www/html/sheduler/all-logs.log', | |
handleExceptions: true, | |
json: true, | |
maxsize: 5242880, //5MB | |
maxFiles: 5, | |
colorize: false | |
}), | |
new winston.transports.Console({ | |
level:'debug', | |
handleExceptions:true, | |
json: false, | |
colorize:true | |
}) | |
], | |
exitOnError:false | |
}); | |
module.exports = logger; | |
var cron = require('cron'); | |
var cronJob = cron.job("* * * * * *", function() | |
{ | |
logger.info("test1"); | |
logger.debug("test2"); | |
}); | |
cronJob.start(); |
This simple script creates a logger to log to the filesystem and to console every second. The logfile path is set to /var/www/html/sheduler/all-log.log
Install sheduler as a systemd service
To make our sheduler reboot save we now add the script as a systemd service.
add a file:
/etc/systemd/system/nodecron.service
paste following script in this service unit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=nodecron service | |
After=network.target | |
[Service] | |
User=root | |
ExecStart=/usr/bin/node /var/www/html/sheduler/index.js | |
[Install] | |
WantedBy=multi-user.target |
Test your systemd-service
At last we reload our service units by typing:$ systemctl --system daemon-reload
Enable your service
$ systemctl enable nodecron
Start your service
$ systemctl start nodecron
Get status
$ systemctl status nodecron
If everything works you will get a status like that:
This tells us, that our logger logs every second a info and a debugmessage.
As you can see, our node.js script gets executed successfully. The service is tunning even, if you reboot the machine.
At the end you will find a logfile with your 2 loggings under:
/var/www/html/sheduler/all-logs.log
Disable your service like that
$ systemctl disable nodecron
Keine Kommentare:
Kommentar veröffentlichen