-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathensure_exit.py
44 lines (32 loc) · 996 Bytes
/
ensure_exit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
import psutil # type: ignore
import threading
from sys import platform
# Define the process name based on the OS
if os.name == "nt": # Windows
process_name = "File Sense.exe"
elif platform.startswith("linux"): # Unix-like systems
process_name = "file-sense"
else:
process_name = "File Sense"
def get_process_id(process_name):
for proc in psutil.process_iter(attrs=["pid", "name"]):
try:
if process_name == proc.info["name"]:
return proc.info["pid"]
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess) as e:
print(e)
return False
def set_interval(func, sec):
def func_wrapper():
set_interval(func, sec)
func()
t = threading.Timer(sec, func_wrapper)
t.start()
return t
parent_pid = os.getpid()
def check():
if get_process_id(process_name) == False:
print("exiting")
psutil.Process(parent_pid).kill()
set_interval(check, 10)