Giter VIP home page Giter VIP logo

pycronic's Introduction

pycronic

Chinese Version

This project is inspired by cronic and includes some extra useful functions such as sending email error report through SMTP and store logs of crontab scripts.

Why pycronic?

Crontab has the ability to send mail notification when any output was generated executing your script, as we know. It will send a bunch of emails to you every day if you have a lof of scripts. What if we only want to get the mail when something goes wrong?

As a result, You may config your crontab like this: :

# Redirect all standard output to /dev/null so we will get an email
# only if this script has some standard error output.
* * * * * some_work > /dev/null

# Or this to ignore all output for lazy people, but you will never 
# be notified if your script fails.
* * * * * some_work > /dev/null 2>&1

Using pycronic make things simplier: :

cronic="/usr/local/bin/cronic"                                                                       
* * * * * &cronic some_work

All you need is to prepend cronic to your script execution command. cronic command will check the return code and the error output for you, if something wents wrong, you will get an email notification through crontab's default mailing system or your customized STMP server. :

MAIL TITLE: [Cronic@server1] Error occoured when running "backup"

MAIL CONTENT: 

Cronic Error Report
===================

[2013-11-12 16:07:24.228788] Cronic detected failure or error output for the command:

backup

RESULT CODE: 2

ERROR OUTPUT:
~~~~~~~~~~~~~

Can not connect to database!

STANDARD OUTPUT:
~~~~~~~~~~~~~~~~

Starting backup...

And cronic will store all your scripts output to a directory (/tmp/pycronic by default).

Installation

Using pip: :

# Install from pypi
sudo pip install pycronic
# Latest version from git

ย  sudo pip install https://github.com/huajigang/pycronic/archive/master.zip

The version in pypi is out.Please don't use the version from pypi,because there is a error in this version,and it dosen't support 'tls/ssl'.

Configuration

After the installation, run "cronic" in your command line to verify: :

$ cronic 
Usage: cronic YOUR_COMMAND

$ cronic ls
Config file "/etc/pycronic.conf" does not exist!
Run "cronic init" to create a default one."

Then run "sudo cronic init" to create a default config file under /etc, the default config file should look like this: :

# Log path for pycronic, pycronic will store all logs to this directory
log_path = /tmp/pycronic

# Send an error email or not, default to not send
send_alert_email = True

# Email Title
mail_title = [Cronic@%(host)s] Error occoured when running "%(command)s"

# Email receivers
# receivers = [email protected]

# Email smtp server config
[email_config_smtp]
# username = username
# host = smtp.sample.com
# password = password
# from = Cronic <[email protected]>
# port = 587
# use_tls = False
# use_ssl = False

How to use

cronic will be silent if no error occured when running a script: :

piglei@macbook-pro:etc$ cronic ls
piglei@macbook-pro:etc$ cat /tmp/pycronic/ls.log 
[The script result will be stored in the log file]

But if an error has occured (cronic will check the standard error output), it will print an error message like this: :

$ cronic ls asdf
Cronic Error Report
===================

[2013-11-12 15:49:03.349575] Cronic detected failure or error output for the command:

ls asdf

RESULT CODE: 1

ERROR OUTPUT: 
~~~~~~~~~~~~~

ls: asdf: No such file or directory

STANDARD OUTPUT:
~~~~~~~~~~~~~~~~

None

If you have configured your crontab an email will send to your email address.

You can also modify config to send mail through SMTP instead of using crontab which is highly more recommended.

Rock crontab

Now config your crontab, using pycronic to wrap your scripts: :

$ crontab -e
# If you have not config your pycronic.conf's smtp config, you can still
# use crontab to send error emails.
MAILTO="[email protected]"
cronic="/usr/local/bin/cronic"                                                                       

*/5 * * * *  $cronic YOUR SCRIPT

Enjoy!

pycronic's People

Contributors

piglei avatar huajigang avatar n3storm avatar phumpal avatar

Watchers

Hua Jigang avatar

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.