Skip to content

Commit

Permalink
Check if another daemon is running
Browse files Browse the repository at this point in the history
  • Loading branch information
vrde committed Feb 16, 2020
1 parent dd529b1 commit f662fd0
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions quiet-toggle-bar.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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

0 comments on commit f662fd0

Please sign in to comment.