Running check (#3)

* Check if the daemon is running by querying the PIDs on the system instead of a marker file

* code folded for better visibility

* code folded for better visibility

* adjust to naming conventions

* remove unused code

* make the `daemon running` warning more simple

Co-authored-by: Velimir <vivel@users.noreply.github.com>
This commit is contained in:
aroundthfur 2020-01-04 23:26:05 +01:00 committed by Adnan Hodzic
parent 0f25cca1ba
commit 83b4ed1c41
1 changed files with 19 additions and 16 deletions

View File

@ -35,9 +35,6 @@ bat_state = p.sensors_battery().power_plugged
# auto-cpufreq log file
auto_cpufreq_log_file = "/var/log/auto-cpufreq.log"
# daemon deployment marker
daemon_marker = "/etc/opt/auto-cpufreq"
# deploy cpufreqctl script
def cpufreqctl():
# deploy cpufreqctl script (if missing)
@ -355,20 +352,27 @@ def read_log():
print("ERROR: auto-cpufreq log is missing.\n\nMake sure to run: \"python3 auto-cpufreq.py --install\" first")
footer(79)
def marker_check():
if os.path.isfile(daemon_marker):
def running_check():
daemon_marker = False
for proc in p.process_iter():
try:
pinfo = proc.as_dict(attrs=['pid', 'name', 'cmdline'])
except p.NoSuchProcess:
pass
else:
if pinfo['name'] == 'python3' and \
'/usr/bin/auto-cpufreq' in pinfo['cmdline'] and '--daemon' in pinfo['cmdline']:
daemon_marker = True
if daemon_marker:
print("\n" + "-" * 25 + " detected auto-cpufreq daemon" + "-" * 25 + "\n")
print("ERROR: prevention from running multiple instances")
print("\nIt seems like auto-cpufreq daemon is already running.\n\nTo view live log run:\nauto-cpufreq --log")
print("\nTo disable and remove auto-cpufreq daemon, run:\nsudo auto-cpufreq --remove")
print("\nIt seems like auto-cpufreq daemon is already running.\n")
print("----")
print("\nTo view live log run:\n\tauto-cpufreq --log")
footer(79)
sys.exit()
def marker_create(f):
f = open(f, "w")
f.write("auto-cpufreq daemon intsall marker")
f.close()
# cli
@click.command()
@click.option("--monitor", is_flag=True, help="Monitor and suggest CPU optimizations")
@ -399,7 +403,7 @@ def cli(monitor, live, daemon, install, log):
subprocess.call("clear")
elif monitor:
while True:
marker_check()
running_check()
root_check()
gov_check()
cpufreqctl()
@ -409,7 +413,7 @@ def cli(monitor, live, daemon, install, log):
subprocess.call("clear")
elif live:
while True:
marker_check()
running_check()
root_check()
gov_check()
cpufreqctl()
@ -420,11 +424,10 @@ def cli(monitor, live, daemon, install, log):
elif log:
read_log()
elif install:
marker_check()
running_check()
root_check()
gov_check()
deploy()
marker_create(daemon_marker)
elif remove:
root_check()
remove()