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
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)
|