Skip to main content

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/piglei/pycronic/archive/master.zip
# Or install from github
sudo pip install -e git+https://github.com/piglei/pycronic/#egg=pycronic

Note: pycronic is now python3 compatible, you can install using pip3 install ...

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 = sample@sample.com

# Email smtp server config
[email_config_smtp]
# username = username
# host = smtp.sample.com
# password = password
# from = Cronic <pycronic@sample.com>
# port = 587

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="piglei2007@gmail.com"
cronic="/usr/local/bin/cronic"

*/5 * * * *  $cronic YOUR SCRIPT

Enjoy!

Metadata

Release files for pycronic 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycronic 0.0.5
File Size Uploaded Provenance Actions
pycronic-0.0.5.tar.gz 5.0 kB Without provenance Details for pycronic-0.0.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pycronic 0.0.5
File Interpreter ABI Platform Actions
pycronic-0.0.5-py3-none-any.whl Python 3 none any Details for pycronic-0.0.5-py3-none-any.whl

Total release size: 17.5 kB

Release files / pycronic-0.0.5.tar.gz

Download URL pycronic-0.0.5.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a4e274b42714d248b79f8ef652a7817c39c731f03dd2435a8f9e62a2c51b65a8
BLAKE2b-256 checksum
How to use checksums
488e2ac849a0284af0cd05a73836fa76f4d1ce1f5457311ec7888efcd545258f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.18.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

Release files / pycronic-0.0.5-py3-none-any.whl

Download URL pycronic-0.0.5-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
88757ff3aa34abd53a3d8f0ee0fdfdb7d1b829b8be9656aa1f24e5859e6fb37c
BLAKE2b-256 checksum
How to use checksums
52edd4e527652e91aa1c2855f3367d3bd9b7b863251170317b89ec4336e50467
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.18.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

Security for pycronic 0.0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page