diff --git a/nwg_panel/main.py b/nwg_panel/main.py index b775b54..00d808d 100644 --- a/nwg_panel/main.py +++ b/nwg_panel/main.py @@ -10,6 +10,7 @@ License: MIT import argparse import signal import sys +import time import gi @@ -239,6 +240,44 @@ def instantiate_content(panel, container, content_list, icons_path=""): def main(): + # Kill running instances, if any + own_pid = os.getpid() + # We should never have more that 1, but just in case + running_instances = [] + + for proc in psutil.process_iter(): + # kill 'nwg-panel', don't kill 'nwg-panel-config' + if "nwg-panel" in proc.name() and "-con" not in proc.name(): + pid = proc.pid + if pid != own_pid: + running_instances.append(pid) + # The easy way: try SIGINT, which we handle gentle + print("Running instance found, PID {}, sending SIGINT".format(pid)) + os.kill(pid, signal.SIGINT) + + # Give it half a second to die + time.sleep(0.5) + + # The hard way, if something's still alive ;) + pids = psutil.pids() + for p in running_instances: + if p in pids: + print("PID {} still alive, sending SIGKILL".format(p)) + os.kill(p, signal.SIGKILL) + + # If started e.g. with 'python /main.py', the process won't be found by name. + # Let's use saved PID and kill mercilessly. This should never happen in normal use. + pid_file = os.path.join(temp_dir(), "nwg-panel.pid") + if os.path.isfile(pid_file): + try: + pid = int(load_text_file(pid_file)) + os.kill(pid, signal.SIGKILL) + print("Running no name instance killed, PID {}".format(pid)) + except: + pass + + save_string(str(own_pid), pid_file) + common.config_dir = get_config_dir() cache_dir = get_cache_dir() if cache_dir: @@ -290,17 +329,6 @@ def main(): global restart_cmd restart_cmd = "nwg-panel -c {} -s {}".format(args.config, args.style) - # Try and kill already running instance if any - pid_file = os.path.join(temp_dir(), "nwg-panel.pid") - if os.path.isfile(pid_file): - try: - pid = int(load_text_file(pid_file)) - os.kill(pid, signal.SIGINT) - print("Running instance killed, PID {}".format(pid)) - except: - pass - save_string(str(os.getpid()), pid_file) - save_string("-c {} -s {}".format(args.config, args.style), os.path.join(local_dir(), "args")) common.app_dirs = get_app_dirs()