Skip to content

Instantly share code, notes, and snippets.

@michaelconnor00
Last active December 12, 2015 00:05
Show Gist options
  • Save michaelconnor00/0ac09077d9cb68b97f14 to your computer and use it in GitHub Desktop.
Save michaelconnor00/0ac09077d9cb68b97f14 to your computer and use it in GitHub Desktop.
Django command to kill runserver. Handy when using Djanog in a docker container and using runserver externally.
import os
import shlex
import subprocess
from subprocess import PIPE, STDOUT
from django.core.management.base import BaseCommand
class Command(BaseCommand):
help = 'Kill local processes running runserver command'
def handle(self, *args, **options):
args = shlex.split('ps aux')
p1 = subprocess.Popen(args, stdin=PIPE, stderr=STDOUT, stdout=PIPE)
for line in p1.stdout:
pid_parts = line.split(' ')
for part in pid_parts:
if part == 'runserver':
# print 'proc: ', line
# print 'kill %s' % pid_parts[7]
try:
int(pid_parts[7]) # check if pid is an int
status = os.system('kill %s' % pid_parts[7])
if status != 0:
print 'Kill FAILED'
except Exception as e:
print 'EXC: ', e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment