From f662fd0d3d5b4c4c3d5344a67d7d4c800430b69a Mon Sep 17 00:00:00 2001 From: vrde Date: Mon, 17 Feb 2020 00:24:03 +0100 Subject: [PATCH] Check if another daemon is running --- quiet-toggle-bar.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/quiet-toggle-bar.py b/quiet-toggle-bar.py index 4e233ba..0e8a0a1 100755 --- a/quiet-toggle-bar.py +++ b/quiet-toggle-bar.py @@ -1,9 +1,23 @@ #!/usr/bin/env python3 +import os +import sys +import argparse +import asyncio from subprocess import run, check_output from i3ipc.aio import Connection from i3ipc import Event -import asyncio + + +def check_daemon(kill=False): + name = os.path.basename(__file__) + pid = os.getpid() + pids = list(filter(lambda p: int(p) != pid, check_output(['pgrep', '-f', name]).split())) + if pids: + if kill: + run(['kill'] + pids) + else: + sys.exit('Another instance is running, kill it or run this command with the --kill option') async def main(): def on_event(self, e): @@ -20,4 +34,14 @@ def on_event(self, e): c.on(Event.WORKSPACE_FOCUS, on_event) await c.main() -asyncio.get_event_loop().run_until_complete(main()) +parser = argparse.ArgumentParser(description='Toggle the statusbar on i3wm.') +parser.add_argument('--kill', dest='kill', action='store_const', + const=True, default=False, + help='Kill any other running instance.') + +args = parser.parse_args() +check_daemon(args.kill) +try: + asyncio.get_event_loop().run_until_complete(main()) +except KeyboardInterrupt: + pass