''' Do not start program, if pidfile exists and is an active process. (c) 2011-2012,2023 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. ''' from __future__ import absolute_import from __future__ import print_function import os, sys, atexit, paths def unlink_pidfile(): try: os.unlink(paths.pid_file) except: pass def exit(msg, es=1, multi=False): if "-h" in sys.argv or "--help" in sys.argv: return elif multi: 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) multi = "-m" in sys.argv or "--multi" in sys.argv 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, multi=multi) else: print("SVPlayer pid file exists, but there is no such process with", end=' ') print("this pid, ignoring.") savepid() elif sys.platform.startswith('win'): exit("Pid file exists, please remove it if SVPlayer crashed.") else: savepid()