Cleanup PID checking logic and write PID logic.

This commit is contained in:
Ryan Kraus 2015-09-01 03:29:07 -04:00
parent 4ca8f184e6
commit 7992882fa3

View File

@ -122,24 +122,19 @@ def check_pid(pid_file):
pid = int(open(pid_file, 'r').readline()) pid = int(open(pid_file, 'r').readline())
except IOError: except IOError:
# PID File does not exist # PID File does not exist
pass return
else:
try: try:
os.kill(pid, 0) os.kill(pid, 0)
except OSError: except OSError:
# PID does not exist # PID does not exist
pass return
else:
# PID already exists
print('Fatal Error: HomeAssistant is already running.') print('Fatal Error: HomeAssistant is already running.')
sys.exit(1) sys.exit(1)
def write_pid(pid_file): def write_pid(pid_file):
""" Create PID File """ """ Create PID File """
# store pid
if pid_file:
# write pid file
pid = os.getpid() pid = os.getpid()
try: try:
open(pid_file, 'w').write(str(pid)) open(pid_file, 'w').write(str(pid))
@ -162,6 +157,7 @@ def main():
check_pid(args.pid_file) check_pid(args.pid_file)
if args.daemon: if args.daemon:
daemonize() daemonize()
if args.pid_file:
write_pid(args.pid_file) write_pid(args.pid_file)
if args.demo_mode: if args.demo_mode: