A Python package for controlling cron job execution
Project description
Cronflow
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
- Import the decorator:
from cronflow import cron_switch
- Apply it to your main function:
@cron_switch
def main():
# Your code here...
print("Processing data...")
if __name__ == "__main__":
main()
- 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
}
- 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
- When a Python script with the
@cron_switchdecorator runs, it checks if theCRONenvironment variable is set - If running from cron, it checks the configuration file to see if the script is enabled
- If disabled, it exits immediately without executing the main logic
- 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
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a4f08c67adb97779c6496c924c5eaba770dab9286649569d56bebab9b7ccda0
|
|
| MD5 |
a53a899ae19e1fb7b4e96ccb7953ade0
|
|
| BLAKE2b-256 |
ee31d2a92bc821b8c8f4173d824a70d24016534ba3d39e542436d665244f373f
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab0f19833624399cb4b3f9ffb7e31a9f830b29f484bfe327190850f3b9bca9f4
|
|
| MD5 |
4c7a110df36f3b2c2158a1a23f18b50e
|
|
| BLAKE2b-256 |
bba2ba011866c069e262896f9ba883d39cfd89d095d5526483bf7bd385797cf2
|