comparison py/tempserver.py @ 499:4a8a1e886a8c

add --hup
author Matt Johnston <matt@ucc.asn.au>
date Fri, 07 Mar 2014 20:35:19 +0800
parents ccc2915b2dd9
children 5eb7e2400c18
comparison
equal deleted inserted replaced
498:ccc2915b2dd9 499:4a8a1e886a8c
106 setup_logging() 106 setup_logging()
107 107
108 heredir = os.path.abspath(os.path.dirname(__file__)) 108 heredir = os.path.abspath(os.path.dirname(__file__))
109 pidpath = os.path.join(heredir, 'tempserver.pid') 109 pidpath = os.path.join(heredir, 'tempserver.pid')
110 pidf = lockfile.pidlockfile.PIDLockFile(pidpath, threaded=False) 110 pidf = lockfile.pidlockfile.PIDLockFile(pidpath, threaded=False)
111 do_hup = '--hup' in sys.argv
111 try: 112 try:
112 pidf.acquire(0) 113 pidf.acquire(0)
113 pidf.release() 114 pidf.release()
114 except lockfile.AlreadyLocked, e: 115 except lockfile.AlreadyLocked, e:
115 pid = pidf.read_pid() 116 pid = pidf.read_pid()
117 if do_hup:
118 try:
119 os.kill(pid, signal.SIGHUP)
120 print>>sys.stderr, "Sent SIGHUP to process %d" % pid
121 sys.exit(0)
122 except OSError:
123 print>>sys.stderr, "Process %d isn't running?" % pid
124 sys.exit(1)
125
116 print>>sys.stderr, "Locked by PID %d" % pid 126 print>>sys.stderr, "Locked by PID %d" % pid
127
117 stale = False 128 stale = False
118 if pid > 0: 129 if pid > 0:
119 if '--new' in sys.argv: 130 if '--new' in sys.argv:
120 try: 131 try:
121 os.kill(pid, 0) 132 os.kill(pid, 0)
139 if stale: 150 if stale:
140 # isn't still running, steal the lock 151 # isn't still running, steal the lock
141 print>>sys.stderr, "Unlinking stale lockfile %s for pid %d" % (pidpath, pid) 152 print>>sys.stderr, "Unlinking stale lockfile %s for pid %d" % (pidpath, pid)
142 pidf.break_lock() 153 pidf.break_lock()
143 154
155 if do_hup:
156 print>>sys.stderr, "Doesn't seem to be running"
157 sys.exit(1)
158
144 if '--daemon' in sys.argv: 159 if '--daemon' in sys.argv:
145 logpath = os.path.join(os.path.dirname(__file__), 'tempserver.log') 160 logpath = os.path.join(os.path.dirname(__file__), 'tempserver.log')
146 logf = open(logpath, 'a+') 161 logf = open(logpath, 'a+')
147 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf): 162 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf):
148 start() 163 start()