Skip to content
Simple Cron Command Implementation in Nodejs
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin
lib
test
.editorconfig
.gitignore
.travis.yml
Dockerfile
LICENSE
Makefile
README.md
index.js
package.json

README.md

Build Status NPM version Dependency Status Downloads Docker Pulls

CRON_JS

Simple cron command implementation in Nodejs

  • Define job schedules in standard crontab-like format
    • Extended to support seconds digit
  • No "daemon mode": all status informations are printed to stdout, including outputs of executed jobs
  • Support email for sending job results: both sendmail command and SMTP are supported

Requirement

  • Nodejs >= 6.0

Usage Example

Write your crontab file as:

*/10 * * * * * echo CRONJOB.mbp `date`

And start cron in foreground with that crontab file:

$ CRON_JS_MAILTO=user@example.com cron ./crontab

cron sends all job outputs to stdout, and also send them to user@example.com.

$ CRON_JS_MAILTO=user@example.com cron ./crontab
[2017-06-15 10:53:09.407] [INFO] [default] - Setting up email notification
[2017-06-15 10:53:09.411] [INFO] [default] - Sender: admin@example.com
[2017-06-15 10:53:09.411] [INFO] [default] - Mailto: user@example.com
[2017-06-15 10:53:09.411] [INFO] [default] - Use sendmail to send emails
[2017-06-15 10:53:09.432] [INFO] [default] - Job registered: {"cronTime":"*/10 * * * * *","command":"echo CRONJOB.mbp `date`"}
[2017-06-15 10:53:10.441] [INFO] [default] - echo CRONJOB.mbp `date` (pid=67086) | Started.
[2017-06-15 10:53:10.449] [INFO] [default] - echo CRONJOB.mbp `date` (pid=67086) | Stdout: CRONJOB.mbp Thu Jun 15 10:53:10 JST 2017
[2017-06-15 10:53:10.449] [INFO] [default] - echo CRONJOB.mbp `date` (pid=67086) | Stderr: 
[2017-06-15 10:53:10.449] [INFO] [default] - echo CRONJOB.mbp `date` (pid=67086) | Exited with code 0
[2017-06-15 10:53:10.449] [DEBUG] [default] - echo CRONJOB.mbp `date` (pid=67086) | Sending email to user@example.com
[2017-06-15 10:53:20.439] [INFO] [default] - echo CRONJOB.mbp `date` (pid=67139) | Started.
[2017-06-15 10:53:20.448] [INFO] [default] - echo CRONJOB.mbp `date` (pid=67139) | Stdout: CRONJOB.mbp Thu Jun 15 10:53:20 JST 2017
[2017-06-15 10:53:20.448] [INFO] [default] - echo CRONJOB.mbp `date` (pid=67139) | Stderr: 
[2017-06-15 10:53:20.448] [INFO] [default] - echo CRONJOB.mbp `date` (pid=67139) | Exited with code 0
[2017-06-15 10:53:20.449] [DEBUG] [default] - echo CRONJOB.mbp `date` (pid=67139) | Sending email to user@example.com
...

Arguments

Command Usage

$ [<EMAIL_OPTIONS= > ...] cron [--exit-on-failure] [--timezone <timeZone>] [<email options> ...] <crontab>
Arguments Required Description
<crontab> Yes crontab file that defines jobs to schedule, one per line
--exit-on-failure No When given and any of jobs ends with status code other than 0, exit cron program with that status code
--timezone <timeZone> No When given, set timezone for cron job definitions
--mailto <address>, --smtp-host <host>, --smtp-port <port>, --smtp-user <user>, --smtp-pass <password> No Email notifiction options: see below for details

Crontab Format

A sample crontab file will look like:

# sec min hour day month day-of-week command
# Runs every weekday (Monday through Friday) at 11:30:00 AM
00 30 11 * * 1-5 echo foo
# Runs 23 minutes 10 seconds after midn, 2am, 4am ..., everyday
10 23 0-23/2 * * * echo bar 1>&2

Where each number represents seconds, munites, hours, day of month, months and day of week respectively. All trailing texts are treated as shell command to execute.

Lines that start with # are treated as comments and are ignored.

Jobs are scheduled with node-cron, so see its document for details about available cron time format.

Specifying environment variables in crontab file is not supported.

Email Options

Following email options are supported.

Email options can be also passed via environment vairbles. When both are provided, values given in commandline argument always take precedence.

Arguments Environment Variable Description
--mailto <address> CRON_JS_MAILTO Email address to send result to
--smtp-host <host> CRON_JS_SMTP_HOST SMTP host to use to send emails
--smtp-port <port> CRON_JS_SMTP_PORT SMTP port to use to send emails
--smtp-user <user> CRON_JS_SMTP_USER SMTP username to use to send emails
--smtp-pass <password> CRON_JS_SMTP_PASS SMTP password to use to send emails

There are two email "mode" for cron:

  • When only --mailto (or CRON_JS_MAILTO) is given of these email options, cron works in "sendmail" mode. In this mode, cron tries to send emails via locally installed sendmail command.

  • If any of SMTP options are given, cron works in "SMTP" mode, where cron tries to send emails using provided STMP configurations.

License

Copyright (c) 2017 10sr 8.slashes@gmail.com

This software is licensed under the Apache License, Version 2.0 , see LICENSE for details.

You can’t perform that action at this time.