disable running multiple instances (needs improvements on snap)

This commit is contained in:
Adnan Hodzic 2020-02-08 17:04:43 +01:00
parent 71a8b92e04
commit 33837c97a3
2 changed files with 28 additions and 25 deletions

View File

@ -41,7 +41,6 @@ def main(monitor, live, daemon, install, log):
countdown(5) countdown(5)
subprocess.call("clear") subprocess.call("clear")
elif os.getenv("PKG_MARKER") != "SNAP": elif os.getenv("PKG_MARKER") != "SNAP":
# ToDo: disable running daemon without install (like on snap)
while True: while True:
root_check() root_check()
gov_check() gov_check()
@ -57,9 +56,8 @@ def main(monitor, live, daemon, install, log):
exit(1) exit(1)
elif monitor: elif monitor:
while True: while True:
# ToDo: doesn't work on snap
running_check()
root_check() root_check()
running_daemon() # doesn't work on snap
gov_check() gov_check()
cpufreqctl() cpufreqctl()
sysinfo() sysinfo()
@ -68,9 +66,8 @@ def main(monitor, live, daemon, install, log):
subprocess.call("clear") subprocess.call("clear")
elif live: elif live:
while True: while True:
# ToDo: doesn't work on snap
running_check()
root_check() root_check()
running_daemon() # doesn't work on snap
gov_check() gov_check()
cpufreqctl() cpufreqctl()
sysinfo() sysinfo()
@ -82,15 +79,15 @@ def main(monitor, live, daemon, install, log):
read_log() read_log()
elif install: elif install:
if os.getenv('PKG_MARKER') == "SNAP": if os.getenv('PKG_MARKER') == "SNAP":
running_check()
root_check() root_check()
running_daemon() # doesn't work on snap
gov_check() gov_check()
s.run("snapctl set daemon=enabled", shell=True) s.run("snapctl set daemon=enabled", shell=True)
s.run("snapctl start --enable auto-cpufreq", shell=True) s.run("snapctl start --enable auto-cpufreq", shell=True)
deploy_complete_msg() deploy_complete_msg()
else: else:
running_check()
root_check() root_check()
running_daemon() # doesn't work on snap
gov_check() gov_check()
deploy() deploy()
deploy_complete_msg() deploy_complete_msg()

View File

@ -388,23 +388,29 @@ def read_log():
print("ERROR: auto-cpufreq log is missing.\n\nMake sure to run: \"auto-cpufreq --install\" first") print("ERROR: auto-cpufreq log is missing.\n\nMake sure to run: \"auto-cpufreq --install\" first")
footer(79) footer(79)
def running_check(): # check if program (argument) is running
daemon_marker = False def is_running(program, argument):
for proc in p.process_iter(): # iterate over all process id's found by psutil
for pid in psutil.pids():
try: try:
pinfo = proc.as_dict(attrs=['pid', 'name', 'cmdline']) # requests the process information corresponding to each process id
except p.NoSuchProcess: p = psutil.Process(pid)
pass # check if value of program-variable that was used to call the function matches the name field of the plutil.Process(pid) output
else: if program in p.name():
if pinfo['name'] == 'python3' and \ # check output of p.name(), output name of program
'/usr/bin/auto-cpufreq' in pinfo['cmdline'] and '--daemon' in pinfo['cmdline']: # p.cmdline() - echo the exact command line via which p was called.
daemon_marker = True for arg in p.cmdline():
if argument in str(arg):
return True
else:
pass
else:
pass
except:
continue
if daemon_marker: # check if auto-cpufreq --daemon is running
print("\n" + "-" * 25 + " detected auto-cpufreq daemon" + "-" * 25 + "\n") def running_daemon():
print("ERROR: prevention from running multiple instances") if is_running('auto-cpufreq', '--daemon'):
print("\nIt seems like auto-cpufreq daemon is already running.\n") deploy_complete_msg()
print("----") exit(1)
print("\nTo view live log run:\n\tauto-cpufreq --log")
footer(79)
sys.exit()