How to simplify a discord bot remote tool After a bit of struggling I've come up with the code

import discord
import os
import subprocess as sub
import psutil

client = discord.Client()

my_path = os.getcwd()
program_instance = False
currently_installed = os.path.isdir(&quot;web-application&quot;)

def run_command(command):
    value = sub.Popen(command, stdout = sub.PIPE, stderr = sub.PIPE)
    return value.communicate()
    
def kill():
    global program_instance
    if not program_instance:
        return &quot;Program instance not initiated&quot;;
    process = psutil.Process(program_instance.pid)
    for proc in process.children(recursive = True):
        proc.kill()
    process.kill()
    program_instance = False
    return &quot;Instance Killed&quot;

def delete_if_exists():
    global currently_installed
    kill()
    if currently_installed:
        os.system(&quot;rmdir /S /Q web-application&quot;)
        while currently_installed:
            currently_installed = os.path.isdir(&quot;web-application&quot;)


def download_from_git():
    #pull from git
    global currently_installed
    kill()
    delete_if_exists()
    instance = sub.Popen(&quot;git clone LINKTOWEBAPPLICATION&quot;)
    instance.communicate()
    currently_installed = True
    return &quot;Successfully downloaded&quot;
    
def run(download = False):
    global program_instance
    folders = run_command(&quot;dir&quot;)
    if not currently_installed or download:
        download_from_git()
    kill()
    path = my_path+&quot;\\web-application&quot;
    #installing values in pipenv
    install_command_instance = sub.Popen(&quot;pipenv install&quot;, cwd = path)
    install_command_instance.communicate()
    
    program_instance = sub.Popen(&quot;pipenv run app.py&quot;, cwd = path)
    return &quot;App Running&quot;

@client.event
async def on_ready():
    print(&quot;Gitty woke&quot;)



@client.event
async def on_message(message):
    if message.author.bot or not message.channel.id == MYDISCORDUSERID:
        return;
    command = message.content

    checker = lambda command_name: command == command_name

    if checker(&quot;run&quot;):
        await message.channel.send(run())

    if checker(&quot;kill&quot;):
        await message.channel.send(kill())

    if checker(&quot;download&quot;):
        await message.channel.send(download_from_git())

    if checker(&quot;run download&quot;):
        await message.channel.send(run(download = True))

    if checker(&quot;ping&quot;):
        currently_installed = os.path.isdir(&quot;web-application&quot;)
        await message.channel.send(&quot;Running:&quot; + f&quot;\n{program_instance=}, {currently_installed=}&quot;)
        
with open(&quot;.key&quot;, &quot;r&quot;) as key:
    client.run(key.read())

The biggest problem with this code was killing running processes. I just wanted to know if there are any easier ways to do this. And if I made any choices which are over complicated.
Edit:
This bot is built to pull a repo from GitHub which contains a web-application that needs to be run. The reason I have to kill the application is so I can download a new version and rerun the code (as multiple instances of the website would be bad). The issue is that when the website is running I also have to be able to send the bot commands and thus have to run it as a separate instance so that the bot doesn't get &quot;clogged&quot; up running the site and can still take inputs
This depends on the nature and purpose of the process.
For example, you might schedule your process as a coroutine, and cancel its execution with Future.cancel: https://docs.python.org/3/library/asyncio-future.html#asyncio.Future.cancel
Your process killing method is aggressive and quite risky, some more context around why you're cancelling processes and the nature of the processes would be useful for providing answers.