Skip to main content

A Python package for controlling cron job execution

Project description

Cronflow

PyPI version Python Versions License

A lightweight Python package for controlling cron job execution with a simple decorator.

Overview

Cronflow allows you to easily enable or disable Python scripts that run via cron without modifying your crontab entries. By using a simple decorator, you can control which scripts run based on a centralized configuration file.

Key Features:

  • Simple decorator syntax for enabling/disabling cron jobs
  • No need to modify crontab entries to enable/disable scripts
  • Centralized configuration for managing multiple scripts
  • Automatic logging of script execution
  • Customizable configuration and log paths

Installation

pip install cronflow

Basic Usage

  1. Import the decorator:
from cronflow import cron_switch
  1. Apply it to your main function:
@cron_switch
def main():
    # Your code here...
    print("Processing data...")

if __name__ == "__main__":
    main()
  1. Create a configuration file:

Create a cron_switch.json file in the same directory as your script:

echo '{"example.py": true}' > cron_switch.json
{
  "your_script.py": true,
  "another_script.py": false
}
  1. Run your script from cron with the CRON environment variable:
CRON=true
0 8 * * * cd /path/to/scripts && python your_script.py

Now your_script.py will only run if it's set to true in the configuration file. You can disable it without modifying your crontab by simply changing the configuration.

Advanced Usage

Custom Configuration Path

You can specify a custom path for the configuration file:

@cron_switch(config_path="/etc/cronflow", config_name="cron_config.json")
def main():
    # Your code here...

Custom Log Directory

Customize where logs are stored:

@cron_switch(log_dir="/var/log/cronflow")
def main():
    # Your code here...

Custom Environment Variable

Use a different environment variable instead of "CRON":

@cron_switch(env_var="SCHEDULED_RUN")
def main():
    # Your code here...

Configuration Management

You can create/edit the cron_switch.json file manually or use a script:

import json

def enable_script(script_name, config_path="cron_switch.json"):
    with open(config_path, "r") as f:
        config = json.load(f)
    
    config[script_name] = True
    
    with open(config_path, "w") as f:
        json.dump(config, f, indent=2)
    
    print(f"Enabled {script_name}")

def disable_script(script_name, config_path="cron_switch.json"):
    with open(config_path, "r") as f:
        config = json.load(f)
    
    config[script_name] = False
    
    with open(config_path, "w") as f:
        json.dump(config, f, indent=2)
    
    print(f"Disabled {script_name}")

How It Works

  1. When a Python script with the @cron_switch decorator runs, it checks if the CRON environment variable is set
  2. If running from cron, it checks the configuration file to see if the script is enabled
  3. If disabled, it exits immediately without executing the main logic
  4. If enabled or not running from cron, it continues with normal execution

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Release history Release notifications | RSS feed

This version

0.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cronflow-0.2.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cronflow-0.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file cronflow-0.2.tar.gz.

File metadata

  • Download URL: cronflow-0.2.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for cronflow-0.2.tar.gz
Algorithm Hash digest
SHA256 1a4f08c67adb97779c6496c924c5eaba770dab9286649569d56bebab9b7ccda0
MD5 a53a899ae19e1fb7b4e96ccb7953ade0
BLAKE2b-256 ee31d2a92bc821b8c8f4173d824a70d24016534ba3d39e542436d665244f373f

See more details on using hashes here.

File details

Details for the file cronflow-0.2-py3-none-any.whl.

File metadata

  • Download URL: cronflow-0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for cronflow-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ab0f19833624399cb4b3f9ffb7e31a9f830b29f484bfe327190850f3b9bca9f4
MD5 4c7a110df36f3b2c2158a1a23f18b50e
BLAKE2b-256 bba2ba011866c069e262896f9ba883d39cfd89d095d5526483bf7bd385797cf2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page