from apscheduler.schedulers.blocking import BlockingScheduler
from datetime import datetime
import pytz
import subprocess
import logging
import psutil
# Set up logging
logging.basicConfig(filename='scheduler.log', level=logging.ERROR)
# Define the function to run the existing script
def run_script():
try:
# Check if the script is already running
pid = get_script_pid()
if pid:
logging.info(f'Script already running with PID {pid}')
else:
subprocess.Popen(["python", "win_eve_logs_transfer.py"])
logging.info('Script started successfully')
except Exception as e:
logging.error(f'Error running script: {str(e)}')
# Define a function to get the process ID of the script
def get_script_pid():
for proc in psutil.process_iter():
try:
if proc.name() == 'python' and 'win_eve_logs_transfer.py' in ' '.join(proc.cmdline()):
return proc.pid
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return None
# Create a scheduler and add the job to run the script every 10 minutes in Ireland time
scheduler = BlockingScheduler(timezone='Europe/Dublin')
scheduler.add_job(run_script, 'interval', minutes=10)
# Start the scheduler
try:
scheduler.start()
except KeyboardInterrupt:
pass