From: James Bunton Date: Tue, 11 Nov 2014 08:44:17 +0000 (+1100) Subject: Handle dead sockets and already-running servers X-Git-Url: https://code.delx.au/bg-scripts/commitdiff_plain/deb3e40c103b740dac866e4d2b3de00114b9ba9e Handle dead sockets and already-running servers --- diff --git a/randombg.py b/randombg.py index 9ff4642..51f6f72 100755 --- a/randombg.py +++ b/randombg.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python2 VERSION = "2.1" @@ -239,7 +239,7 @@ class Cycler(object): self.cache_filename = options.cache_filename logging.debug("Initialising wallchanger") - wallchanger.init(options.background_colour, options.permanent, options.convert) + wallchanger.init(options.background_colour, options.convert) logging.debug("Initialising file list") if options.all_random: @@ -369,9 +369,11 @@ 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: + print >>sys.stderr, "Server is already running! Sending exit command." + sock = sock.makefile() + sock.write("cmd exit\n") + sock.close() + except Exception, e: pass try: @@ -379,22 +381,15 @@ def do_server(options, paths): except OSError: pass + cycler = Cycler() + listener = Listener(options.socket_filename, cycler) + # Initialisation of Cycler delayed so we grab the socket quickly + cycler.init(options, paths) try: - cycler = Cycler() - listener = Listener(options.socket_filename, cycler) - # Initialisation of Cycler delayed so we grab the socket quickly - cycler.init(options, paths) - try: - asyncsched.loop() - except KeyboardInterrupt: - print - cycler.finish() - finally: - # Make sure that the socket is cleaned up - try: - os.unlink(options.socket_filename) - except OSError: - pass + asyncsched.loop() + except KeyboardInterrupt: + print + cycler.finish() def do_client(options, args): if len(args) == 0: @@ -420,9 +415,6 @@ def build_parser(): "\n(client) %prog [options] [next|prev|rescan|reload|pause] [...]" "\nThe first instance to be run will be the server.\n" ) - parser.add_option("-p", "--permanent", - action="store_true", dest="permanent", default=False, - help="Make the background permanent. Note: This will cause all machines logged in with this account to simultaneously change background [Default: %default]") parser.add_option("-v", '-d', "--verbose", "--debug", action="count", dest="verbose", default=0, help="Make the louder (good for debugging, or those who are curious)")