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