From: James Bunton Date: Wed, 29 Feb 2012 02:07:04 +0000 (+1100) Subject: Don't allow two copies of the server at once X-Git-Url: https://code.delx.au/bg-scripts/commitdiff_plain/8d3a3ac522e9cc9bae53f377bd3118088b3d3052 Don't allow two copies of the server at once --- diff --git a/randombg.py b/randombg.py index 4727b20..9ff4642 100755 --- a/randombg.py +++ b/randombg.py @@ -354,10 +354,6 @@ class Listener(asyncore.dispatcher): asyncore.dispatcher.__init__(self) self.cycler = cycler self.create_socket(socket.AF_UNIX, socket.SOCK_STREAM) - try: - os.unlink(socket_filename) - except OSError: - pass self.bind(socket_filename) self.listen(2) # Backlog = 2 @@ -370,6 +366,19 @@ class Listener(asyncore.dispatcher): def do_server(options, paths): + try: + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + sock.connect(options.socket_filename) + print >>sys.stderr, "Server is already running! Will do nothing" + return + except: + pass + + try: + os.unlink(options.socket_filename) + except OSError: + pass + try: cycler = Cycler() listener = Listener(options.socket_filename, cycler) @@ -384,7 +393,7 @@ def do_server(options, paths): # Make sure that the socket is cleaned up try: os.unlink(options.socket_filename) - except: + except OSError: pass def do_client(options, args):