From 7992882fa3adf1adcfa37eef7d083c3d16979ed8 Mon Sep 17 00:00:00 2001 From: Ryan Kraus Date: Tue, 1 Sep 2015 03:29:07 -0400 Subject: [PATCH] Cleanup PID checking logic and write PID logic. --- homeassistant/__main__.py | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/homeassistant/__main__.py b/homeassistant/__main__.py index e219b973f37..6e12afa46c0 100644 --- a/homeassistant/__main__.py +++ b/homeassistant/__main__.py @@ -122,30 +122,25 @@ def check_pid(pid_file): pid = int(open(pid_file, 'r').readline()) except IOError: # PID File does not exist - pass - else: - try: - os.kill(pid, 0) - except OSError: - # PID does not exist - pass - else: - # PID already exists - print('Fatal Error: HomeAssistant is already running.') - sys.exit(1) + return + + try: + os.kill(pid, 0) + except OSError: + # PID does not exist + return + print('Fatal Error: HomeAssistant is already running.') + sys.exit(1) def write_pid(pid_file): """ Create PID File """ - # store pid - if pid_file: - # write pid file - pid = os.getpid() - try: - open(pid_file, 'w').write(str(pid)) - except IOError: - print('Fatal Error: Unable to write pid file {}'.format(pid_file)) - sys.exit(1) + pid = os.getpid() + try: + open(pid_file, 'w').write(str(pid)) + except IOError: + print('Fatal Error: Unable to write pid file {}'.format(pid_file)) + sys.exit(1) def main(): @@ -162,7 +157,8 @@ def main(): check_pid(args.pid_file) if args.daemon: daemonize() - write_pid(args.pid_file) + if args.pid_file: + write_pid(args.pid_file) if args.demo_mode: hass = bootstrap.from_config_dict({