''' Do not start program, if pidfile exists and is an active process. (c) 2011-2012 Jan ONDREJ (SAL) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. ''' import os, sys, atexit, paths def unlink_pidfile(): try: os.unlink(paths.pid_file) except: pass def exit(msg, es=1): if "-m" in sys.argv or "--multi" in sys.argv: print "SVplayer process already running, but multiple instances allowed." else: print msg sys.exit(es) def savepid(): open(paths.pid_file, "wt").write(pid) atexit.register(unlink_pidfile) pid = str(os.getpid()) if os.path.isfile(paths.pid_file): fpid = open(paths.pid_file, "rt").read().strip() # check if process really exists if sys.platform.startswith('linux'): if os.path.exists("/proc/%s" % fpid): exit("SVPlayer pid [%s] already running, exiting..." % fpid) else: print "SVPlayer pid file exists, but there is no such process with", print "this pid, ignoring." savepid() elif sys.platform.startswith('win'): exit("Pid file exists, please remove it if SVPlayer crashed.") else: savepid()