Skip to content

Repository files navigation

Python Scripts Cron Scheduler

Abstraction for managing the scheduling of Python scripts through CronJobs with a out-of-the-box Streamlit Panel.

PythonLinuxStreamlit

Tests StatusCoverage Status

📌 Installation

pip install python_scs

If you want to use the Streamlit panel:

pip install python_scs[streamlit]

🚀 Configuration

Check out the full example.

  1. Make sure you have the following directory structure:
├── scripts/
│ ├── logs/
│ ├── script_test.py
│ ├── __init__.py
├── streamlit_pannel.py
  1. Instantiate the script manager and configure the panel:
# streamlit_pannel.pyfrompython_scsimportPythonScriptsCronManager, streamlit_uimanager=PythonScriptsCronManager(
user=True
)
streamlit_pannel=streamlit_ui.init(
manager,
layout='wide',
title='Scripts Manager',
subheader='Manage your python scripts'
)
  1. Run the panel using streamlit:
streamlit run streamlit_pannel.py

🛠 Using the API

Instantiating the script manager

importosfrompython_scsimportPythonScriptsCronManagerscripts_manager=PythonScriptsCronManager(
config=PythonScriptsCronManager.Config(
app_path=os.path.abspath("."), # Root directory where scripts_folder is locatedscripts_folder="scripts", # Directory containing scriptslogs_folder="scripts/logs"# Directory for logs
),
user=True
)

📌 Check the python-crontab documentation to understand the user parameter.

Listing available scripts

scripts=scripts_manager.get_scripts()
print(scripts) # ["script_test.py"]

Creating a schedule

job=scripts_manager.set_script_job(
script_name="script_test.py",
schedule=["* * * * *"],
comment="Test schedule",
enable=True
)
# Creating a schedule with a custom UNIX commandjob=scripts_manager.set_job(
command='echo "Teste"',
schedule=["* * * * *"],
log_file_name="test.txt", # Required to store outputcomment="Custom schedule",
enable=True
)

📌 To verify if the schedule was created, run:

crontab -l

Listing configured schedules

jobs=scripts_manager.get_jobs()
forjobinjobs:
print(f"{job.comment} - {job.script_name} - {job.is_runing()}")
# Fetching a scheduled job by filtersjob_script_test=scripts_manager.get_job({
"script_name": "script_test.py",
"comment": "Test schedule"
})

Enabling, disabling, executing, and removing a scheduled job

job=scripts_manager.get_job({
"script_name": "script_test.py",
"comment": "Test schedule"
})
job.enable_job() # Enables the jobjob.disable_job() # Disables the jobjob.toggle_job() # Toggles between enabled/disabled# Manually execute the scriptscripts_manager.execute(job)
# Execute as a subprocessscripts_manager.execute(job, use_subprocess=True)
# Remove the schedulescripts_manager.remove_job(job)

📜 License

This project is distributed under the MIT license. See the LICENSE file for more details.

About

Abstração com Painel em Streamlit out-of-the-box para gestão do agendamento de scripts em Python através de CronJobs.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages