This commit is contained in:
PaulVua
2025-02-04 18:00:01 +01:00
parent c6073b49b9
commit a90552148c
2 changed files with 64 additions and 1 deletions

1
.gitignore vendored
View File

@@ -14,3 +14,4 @@ NPM/data/*.txt
NPM/data/*.json NPM/data/*.json
*.lock *.lock
sqlite/*.db sqlite/*.db
tests/

62
master.py Normal file
View File

@@ -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)