Dateien nach "/" hochladen
This commit is contained in:
parent
c6a6f6b591
commit
43d9a4c3ec
1 changed files with 73 additions and 0 deletions
73
background_job.py
Normal file
73
background_job.py
Normal file
|
@ -0,0 +1,73 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
from time import sleep
|
||||
import psutil
|
||||
from plyer import notification
|
||||
|
||||
|
||||
def ram_perc():
|
||||
return psutil.virtual_memory()[2]
|
||||
|
||||
|
||||
def swap_perc():
|
||||
return psutil.swap_memory()[3]
|
||||
|
||||
|
||||
def get_pwmmu():
|
||||
pwmmu = ["", 0, 0] # pwmmu = Process With Most Memory Usage
|
||||
|
||||
for process in psutil.process_iter():
|
||||
try:
|
||||
process_name = process.name()
|
||||
process_usage = process.memory_info().vms / (1024 * 1024)
|
||||
process_id = process.pid
|
||||
|
||||
if process_usage > pwmmu[1]:
|
||||
pwmmu = [process_name, process_usage, process_id]
|
||||
|
||||
except:
|
||||
pass
|
||||
|
||||
print(pwmmu)
|
||||
return pwmmu
|
||||
|
||||
|
||||
def kill_process(process_pid):
|
||||
for process in psutil.process_iter():
|
||||
if process.pid == process_pid:
|
||||
process.kill()
|
||||
|
||||
|
||||
def ram_almost_full():
|
||||
notification.notify(
|
||||
title="Your RAM is almost full",
|
||||
message=f"Your RAM usage is over 90%!\nRam usage: {ram_perc()}%\nSwap usage: {swap_perc()}%",
|
||||
timeout=10,
|
||||
)
|
||||
|
||||
|
||||
def ram_full():
|
||||
pwmmu = get_pwmmu()
|
||||
kill_process(pwmmu[2])
|
||||
|
||||
notification.notify(
|
||||
title="Your memory is full, killed process with most memory usage.",
|
||||
message=f"Your memory usage was higher than 98%, automatically killed process with most memory usage.\nRam usage: {ram_perc()}%\nSwap usage: {swap_perc()}%\n Killed process: {pwmmu[0]}",
|
||||
timeout=10,
|
||||
)
|
||||
|
||||
|
||||
def loop():
|
||||
while True:
|
||||
ram_usage = ram_perc()
|
||||
swap_usage = swap_perc()
|
||||
|
||||
if ram_usage >= 98 and swap_usage >= 98:
|
||||
ram_full()
|
||||
|
||||
elif ram_usage >= 90:
|
||||
ram_almost_full()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
loop()
|
Loading…
Reference in a new issue