A module for creating, reading, updating, and deleting system cron jobs
$ npm install crontabrequire('crontab').load(function(err,crontab){// create with string expressionvarjob=crontab.create('ls -la','0 7 * * 1,2,3,4,5');// create with Datevarjob=crontab.create('ls -lh',newDate(1400373907766));// create with commentvarjob=crontab.create('ls -lt',null,'comment 2');// create special: @reboot, @hourly, @daily, @weekly, @monthly, @yearly, @annually, @midnightvarjob=crontab.create('ls -la','@reboot');// check validvarjob=crontab.create();if(job==null){console.log('failed to create job');}// remove objectvarjob=crontab.create('ls -lr','0 7 * * 1,2,3,4,5','comment 3');crontab.remove(job);// remove conditionscrontab.remove({command:'ls -lh',comment:/comment2/});// manipulate: every business hourvarjob=crontab.create('ls -l');job.minute().at(0);job.hour().between(8,17);job.dow().between('mon','fri');// manipulate: every other hour on weekday nightsvarjob=crontab.create('ls -l');job.hour().between(19,0).every(2);job.hour().between(0,6).every(2);job.dow().between('mon','fri');// manipulate: summervarjob=crontab.create('ls -l');job.month().between('jun','sep');// manipulate: Christmasvarjob=crontab.create('ls -l');job.minute().at(30);job.hour().at(9);job.dom().on(24);job.month().in('dec');// show all jobsvarjobs=crontab.jobs();// show jobs with conditionsvarjobs=crontab.jobs({command:'ls -l',comment:/comment1/});// reset jobs to their original statecrontab.reset();// savecrontab.save(function(err,crontab){});console.log(crontab);});require('crontab').load(function(err,crontab){// get all env variablesvarvars=crontab.vars();// find env variables by namevarvars=crontab.vars({name: 'FOO'});// find env variables by valuevarvars=crontab.vars({val: 'bar'});// find env variables by name and valuevarvars=crontab.vars({name: 'FOO',val: 'bar'});// find env variables by namevarvars=crontab.vars('FOO');// create with a pair of argumentscrontab.vars().add('FOO','foo');// create multiple with an object argumentcrontab.vars().add({'FOO':'foo','BAR':'1'});// remove all env variablescrontab.vars().rm();// remove selected env variablescrontab.vars({name: 'FOO'}).rm();// savecrontab.save(function(err,crontab){});});require('crontab').load(function(err,crontab){if(err){returnconsole.error(err);}varcommand='ls -l';crontab.remove({command:command});crontab.create(command,'@reboot');crontab.save(function(err,crontab){});});require('crontab').load(function(err,crontab){if(err){returnconsole.error(err);}varuuid='64d967a0-120b-11e0-ac64-0800200c9a66';varnodePath=process.execPath.split('/').slice(0,-1).join('/');varexportCommand='export PATH='+nodePath+':$PATH';varforeverCommand=require('path').join(__dirname,'node_modules','forever','bin','forever');varsysCommand=exportCommand+' && '+foreverCommand+' start '+__filename;crontab.remove({comment:uuid});crontab.create(sysCommand,'@reboot',uuid);crontab.save(function(err,crontab){console.log(err)});});// when executing for another user the library uses sudo, unless the// current process runs as rootrequire('crontab').load('alice',function(err,crontab){if(err){returnconsole.error(err);}crontab.save(function(err,crontab){console.log(err)});});Blagovest Dachev
- Blagovest Dachev (2010-2014)
- Martin Owens (2009-2012)
This is a JavaScript port of a Python package by Martin Owens
GPL3

