From a90552148cd3775dcfd67b1bfaae99a0eccc125e Mon Sep 17 00:00:00 2001 From: PaulVua Date: Tue, 4 Feb 2025 18:00:01 +0100 Subject: [PATCH] update --- .gitignore | 3 ++- master.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 master.py diff --git a/.gitignore b/.gitignore index cdbec4c..cfd2a16 100755 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ envea/data/*.json NPM/data/*.txt NPM/data/*.json *.lock -sqlite/*.db \ No newline at end of file +sqlite/*.db +tests/ \ No newline at end of file diff --git a/master.py b/master.py new file mode 100644 index 0000000..22368b3 --- /dev/null +++ b/master.py @@ -0,0 +1,62 @@ +''' +Master Python script that will trigger other scripts at every chosen time pace +This script is triggered as a systemd service used as an alternative to cronjobs + +-->sudo nano /etc/systemd/system/sensor_manager.service + +[Unit] +Description=Master manager for the Python loop scripts +After=network.target + +[Service] +ExecStart=/usr/bin/python3 /var/www/nebuleair_pro_4g/master.py +Restart=always +User=root +WorkingDirectory=/var/www/nebuleair_pro_4g +StandardOutput=append:/var/log/master_manager.log +StandardError=append:/var/log/master_manager_error.log + +[Install] +WantedBy=multi-user.target + +''' +import time +import threading +import subprocess +import json +import os + +# Base directory where scripts are stored +SCRIPT_DIR = "/var/www/nebuleair_pro_4g/tests/" +CONFIG_FILE = "/var/www/nebuleair_pro_4g/config.json" + +def load_config(): + """Load the configuration file to determine which scripts to run.""" + with open(CONFIG_FILE, "r") as f: + return json.load(f) + +def run_script(script_name, interval): + """Run a script in a loop with a delay.""" + script_path = os.path.join(SCRIPT_DIR, script_name) # Build full path + while True: + config = load_config() + if config.get(script_name, True): # Default to True if not found + subprocess.run(["python3", script_path]) + time.sleep(interval) + +# Define scripts and their execution intervals (seconds) +SCRIPTS = [ + ("script1.py", 60), # Runs every 60 seconds + ("script2.py", 10), # Runs every 10 seconds + ("script3.py", 10), # Runs every 10 seconds +] + +# Start threads for enabled scripts +for script_name, interval in SCRIPTS: + thread = threading.Thread(target=run_script, args=(script_name, interval), daemon=True) + thread.start() + +# Keep the main script running +while True: + time.sleep(1) +