start.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env python3
  2. import subprocess
  3. import os
  4. import sys
  5. PRO_PATH = "/platform/LAS/"
  6. LOGFILE = PRO_PATH + "LAS.log"
  7. PIDFILE = PRO_PATH + "LAS.pid"
  8. def is_running(pid):
  9. try:
  10. os.kill(pid, 0)
  11. return True
  12. except OSError:
  13. return False
  14. def start():
  15. if os.path.exists(PIDFILE):
  16. with open(PIDFILE, "r") as f:
  17. pid = int(f.read())
  18. if is_running(pid):
  19. print(f"LAS is already running with PID {pid}")
  20. return
  21. else:
  22. print("Found stale PID file. Cleaning up.")
  23. os.remove(PIDFILE)
  24. with open(LOGFILE, "a") as log:
  25. process = subprocess.Popen(
  26. [sys.executable, "LAS.py"],
  27. stdout=log,
  28. stderr=log,
  29. stdin=subprocess.DEVNULL,
  30. close_fds=True,
  31. )
  32. with open(PIDFILE, "w") as f:
  33. f.write(str(process.pid))
  34. print(f"LAS started with PID {process.pid}")
  35. if __name__ == "__main__":
  36. start()