diff --git a/tools/run-dev.py b/tools/run-dev.py index b57905e642..c73e9c78ad 100755 --- a/tools/run-dev.py +++ b/tools/run-dev.py @@ -129,6 +129,18 @@ if options.clear_memcached: # and all of the processes they spawn. os.setpgrp() +# Save pid of parent process to the pid file. It can be used later by +# tools/stop-run-dev to kill the server without having to find the +# terminal in question. +pid_file_path = os.path.join(os.path.join(os.getcwd(), 'var/run/run_dev.pid')) + +# Required for compatibility python versions. +if not os.path.exists(os.path.dirname(pid_file_path)): + os.makedirs(os.path.dirname(pid_file_path)) +pid_file = open(pid_file_path, 'w+') +pid_file.write(str(os.getpgrp()) + "\n") +pid_file.close() + # Pass --nostatic because we configure static serving ourselves in # zulip/urls.py. cmds = [['./tools/compile-handlebars-templates', 'forever'], @@ -388,3 +400,5 @@ except: finally: # Kill everything in our process group. os.killpg(0, signal.SIGTERM) + # Remove pid file when development server closed correctly. + os.remove(pid_file_path) diff --git a/tools/stop_run_dev b/tools/stop_run_dev new file mode 100755 index 0000000000..c9ff534c19 --- /dev/null +++ b/tools/stop_run_dev @@ -0,0 +1,30 @@ +#!/usr/bin/env python +from __future__ import print_function + +import os +import signal +import sys + +os.chdir(os.path.join(os.path.dirname(__file__), '..')) +pid_file_path = os.path.join(os.path.join(os.getcwd(), 'var/run/run_dev.pid')) + +try: + with open(pid_file_path, 'r') as pid_file: + try: + pid = int(pid_file.read()) + except ValueError: + print('PID value is not an integer!') + sys.exit(1) +except Exception as e: + print("PID file can't be opened!") + print(e) + sys.exit(1) + +# Kill development server process group. +try: + os.killpg(pid, signal.SIGTERM) +except OSError as e: + print(e) + sys.exit(1) + +print("Done")