Skip to content

KillCommand

Base classes

Name Children Inherits
SlashedCommand
slashed.commands
Base class for declarative commands.

⋔ Inheritance diagram

graph TD
  3138582556["system.KillCommand"]
  807245086["commands.SlashedCommand"]
  900363605["base.BaseCommand"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  807245086 --> 3138582556
  900363605 --> 807245086
  925645236 --> 900363605
  2751680898 --> 925645236

🛈 DocStrings

Bases: SlashedCommand

Kill a running process.

Usage

/kill

Kill process by PID or name. Numbers are treated as PIDs, anything else as process name.

Examples:

/kill 1234 # Kill by PID /kill notepad.exe # Kill all processes with this name

Source code in src/slashed/builtin/system.py
class KillCommand(SlashedCommand):
    """Kill a running process.

    Usage:
      /kill <pid_or_name>

    Kill process by PID or name. Numbers are treated as PIDs,
    anything else as process name.

    Examples:
      /kill 1234        # Kill by PID
      /kill notepad.exe # Kill all processes with this name
    """

    name = "kill"
    category = "system"

    def is_available(self) -> bool:
        return find_spec("psutil") is not None

    async def execute_command(self, ctx: CommandContext[Any], target: str) -> None:
        """Kill a process by PID or name."""
        import psutil

        # Try to parse as PID first
        try:
            if target.isdigit():
                pid = int(target)
                process = psutil.Process(pid)
                process.terminate()
                await ctx.print(f"✅ Process **{pid}** terminated")
                return
        except psutil.NoSuchProcess as e:
            msg = f"No process with PID {target}"
            raise CommandError(msg) from e
        except psutil.AccessDenied as e:
            msg = f"Permission denied to kill process {target}"
            raise CommandError(msg) from e

        # If not a number, treat as process name
        killed = 0
        for proc in psutil.process_iter(["pid", "name"]):
            try:
                if proc.info["name"].lower() == target.lower():
                    proc.terminate()
                    killed += 1
            except psutil.NoSuchProcess, psutil.AccessDenied:
                continue

        if killed:
            await ctx.print(f"✅ Terminated **{killed}** process(es) named `{target}`")
        else:
            msg = f"No processes found with name {target!r}"
            raise CommandError(msg)

execute_command async

execute_command(ctx: CommandContext[Any], target: str) -> None

Kill a process by PID or name.

Source code in src/slashed/builtin/system.py
async def execute_command(self, ctx: CommandContext[Any], target: str) -> None:
    """Kill a process by PID or name."""
    import psutil

    # Try to parse as PID first
    try:
        if target.isdigit():
            pid = int(target)
            process = psutil.Process(pid)
            process.terminate()
            await ctx.print(f"✅ Process **{pid}** terminated")
            return
    except psutil.NoSuchProcess as e:
        msg = f"No process with PID {target}"
        raise CommandError(msg) from e
    except psutil.AccessDenied as e:
        msg = f"Permission denied to kill process {target}"
        raise CommandError(msg) from e

    # If not a number, treat as process name
    killed = 0
    for proc in psutil.process_iter(["pid", "name"]):
        try:
            if proc.info["name"].lower() == target.lower():
                proc.terminate()
                killed += 1
        except psutil.NoSuchProcess, psutil.AccessDenied:
            continue

    if killed:
        await ctx.print(f"✅ Terminated **{killed}** process(es) named `{target}`")
    else:
        msg = f"No processes found with name {target!r}"
        raise CommandError(msg)

:fa-brands-github: Show source on GitHub