From 1e23b54b4cd41b5cf68c32e7eea6a50d03f185ae Mon Sep 17 00:00:00 2001 From: Lucio Zambon <lucio.zambon@elettra.eu> Date: Thu, 27 Mar 2025 03:19:05 +0000 Subject: [PATCH] Add new file --- launcher.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 launcher.py diff --git a/launcher.py b/launcher.py new file mode 100644 index 0000000..186f20d --- /dev/null +++ b/launcher.py @@ -0,0 +1,54 @@ +import subprocess +import sys +import psutil +import socket +host = socket.gethostname() +import urllib.request + +if len(sys.argv)<2: + sys.exit('Error: missing executable name') + +sys.argv[1] = sys.argv[1].replace('puma://', '') +feedback = "http://puma-01.elettra.eu/knob/index.php?host="+host+"&pid=" + +# Path to the executable +executable_path = ('/runtime/bin/'+sys.argv[1]).replace('/bin/elettra/', '/elettra/bin/') + +def is_process_running(process_name, params): + """Check if a process with the given name and parameters is running.""" + for proc in psutil.process_iter(['name', 'cmdline']): + try: + # Check if the process name matches + if proc.info['name'] == process_name: + # Check if the command line arguments match + if params == proc.info['cmdline'][1:]: # Skip the first element (the executable name) + return True + except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): + pass + return False + +process_name = sys.argv[1].replace('elettra/', '') +params = sys.argv[2:] +if is_process_running(process_name, params): + print(f"The process '{process_name}' with parameters {params} is already running.") + sys.argv[1] = executable_path + # processInfo = subprocess.Popen('/usr/bin/python3 /home/elettra/tmp/lz/vibration.py '+' '.join(sys.argv[1:]), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + # print(processInfo.stdout.read()) + f = urllib.request.urlopen(feedback+'0') + myfile = f.read() +else: + print(f"The process '{process_name}' with parameters {params} is not running.") + try: + sys.argv[1] = executable_path + process = subprocess.Popen(sys.argv[1:]) + pid = process.pid + print(f"The PID of the launched process is: {pid}") + f = urllib.request.urlopen(feedback+str(pid)) + myfile = f.read() + #print(myfile) + + except Exception as e: + print(e) + f = urllib.request.urlopen(feedback+'-1&err='+str(e)) + myfile = f.read() + #print(myfile) -- GitLab