Coder Social home page Coder Social logo

cron-cli-js's Introduction

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:

$ [email protected] cron ./crontab

cron sends all job outputs to stdout, and also send them to [email protected].

$ [email protected] 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: [email protected]
[2017-06-15 10:53:09.411] [INFO] [default] - Mailto: [email protected]
[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 [email protected]
[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 [email protected]
...

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 [email protected]

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

cron-cli-js's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

cron-cli-js's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.