Skip to content

ProcessesCommand

Base classes

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

⋔ Inheritance diagram

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

🛈 DocStrings

Bases: SlashedCommand

List running processes.

Usage

/ps [--filter_by ]

Shows PID, name, memory usage and status for each process. Optionally filter by process name.

Source code in src/slashed/builtin/system.py
class ProcessesCommand(SlashedCommand):
    """List running processes.

    Usage:
      /ps [--filter_by <name>]

    Shows PID, name, memory usage and status for each process.
    Optionally filter by process name.
    """

    name = "ps"
    category = "system"

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

    async def execute_command(
        self,
        ctx: CommandContext[Any],
        *,
        filter_by: str | None = None,
    ) -> None:
        """List running processes."""
        import psutil

        processes: list[dict[str, Any]] = []
        for process in psutil.process_iter(["pid", "name", "status", "memory_percent"]):
            try:
                pinfo = process.info
                if not filter_by or filter_by.lower() in pinfo["name"].lower():
                    processes.append(pinfo)
            except psutil.NoSuchProcess, psutil.AccessDenied:
                continue

        if not processes:
            await ctx.print("❌ No matching processes found")
            return

        # Sort by memory usage
        processes.sort(key=lambda x: x["memory_percent"], reverse=True)

        # Format as markdown table
        lines = [
            "## Running Processes",
            "",
            "| PID | MEM% | STATUS | NAME |",
            "|-----|------|--------|------|",
        ]

        # Add processes (limit to top 20)
        for proc in processes[:20]:
            lines.append(  # noqa: PERF401
                f"| {proc['pid']} | {proc['memory_percent']:.1f}% | {proc['status']} | {proc['name']} |"  # noqa: E501
            )

        await ctx.print("\n".join(lines))

execute_command async

execute_command(ctx: CommandContext[Any], *, filter_by: str | None = None) -> None

List running processes.

Source code in src/slashed/builtin/system.py
async def execute_command(
    self,
    ctx: CommandContext[Any],
    *,
    filter_by: str | None = None,
) -> None:
    """List running processes."""
    import psutil

    processes: list[dict[str, Any]] = []
    for process in psutil.process_iter(["pid", "name", "status", "memory_percent"]):
        try:
            pinfo = process.info
            if not filter_by or filter_by.lower() in pinfo["name"].lower():
                processes.append(pinfo)
        except psutil.NoSuchProcess, psutil.AccessDenied:
            continue

    if not processes:
        await ctx.print("❌ No matching processes found")
        return

    # Sort by memory usage
    processes.sort(key=lambda x: x["memory_percent"], reverse=True)

    # Format as markdown table
    lines = [
        "## Running Processes",
        "",
        "| PID | MEM% | STATUS | NAME |",
        "|-----|------|--------|------|",
    ]

    # Add processes (limit to top 20)
    for proc in processes[:20]:
        lines.append(  # noqa: PERF401
            f"| {proc['pid']} | {proc['memory_percent']:.1f}% | {proc['status']} | {proc['name']} |"  # noqa: E501
        )

    await ctx.print("\n".join(lines))

:fa-brands-github: Show source on GitHub