BLOG POSTS
    MangoHost Blog / Explore Running Processes with ps and pstree: A Practical Guide
Explore Running Processes with ps and pstree: A Practical Guide

Explore Running Processes with ps and pstree: A Practical Guide

Table of Contents

What are ps and pstree, and Why Should You Care?

Every Linux (or Unix) system is basically a party of running processes. Some are invited, others crash the party. If you’re running a VPS, a dedicated server, or a containerized environment, you need to know what’s going on inside. Enter ps (process status) and pstree (process tree) – your go-to dynamic duo for process exploration and troubleshooting. This guide is your backstage pass to mastering these tools, so you can spot suspicious guests, optimize performance, and keep your setup humming.

Whether you’re a coder, DevOps wrangler, sysadmin, or just a curious server tinkerer, knowing how to map out and interrogate your running processes is fundamental. It’s like a superpower for debugging, performance tuning, and security. Let’s break down how ps and pstree work, how to get them rolling in seconds, and how to wield them like a pro.

A Real-World Admin Horror Story

Picture this: You’ve just spun up a fresh cloud server. Everything’s running smoothly until, one fateful Monday, CPU usage spikes to 100%. Users are screaming, services are timing out, and you’re stuck in a command-line staring contest. You try top – it’s a blur. But with ps and pstree, you can see the culprit: a zombie script spawning child processes like rabbits. Crisis averted. Moral: Know your processes, or your processes will know you (and not in a good way).

How Do ps and pstree Work? The Inside Scoop

  • ps: Think of this as your “process snapshot” camera. Every time you run ps, it gives you a list of current processes – who’s running, how they started, and what they’re up to.
  • pstree: Now imagine a family tree, but for processes. pstree shows you the hierarchical relationship between processes, making it obvious who spawned what (and if anything is running wild).

How do they know? Both tools read from the /proc pseudo-filesystem, which the Linux kernel updates in real-time. Every process gets its own directory under /proc (by PID). ps and pstree simply format and present this info in human-friendly ways.

Structure and Algorithms (But Simple!)

  • ps collects process info (name, PID, parent PID, user, CPU/mem usage, etc.), and prints it as a table.
  • pstree reads the parent-child relationships (PPID -> PID) and draws a tree, showing ancestry and sibling processes.

Both are fast, lightweight, and available out-of-the-box on nearly every Linux distribution. No drama, no dependencies.

Use Cases and Benefits – The Process Detective’s Toolkit

  • Debugging: Easily spot runaway or orphaned processes, hung services, or memory leaks.
  • Security: Identify unknown or suspicious processes (potential malware, crypto miners, or backdoors).
  • Performance: Find resource hogs by sorting processes by CPU or memory usage.
  • Container/Docker Management: Map processes inside containers, track parent processes, and debug init systems.
  • Automation/Scripting: Use ps output in shell scripts to automate process management or monitoring.
  • Forensics: Trace back how a process started (i.e., was it spawned by SSH, cron, or another daemon?).

With these tools, you can become the Sherlock Holmes of your server. Mysterious lag? Unwanted background jobs? ps and pstree will help you crack the case.

Lightning Setup Guide: ps and pstree in Action

Step 1: Installation (Spoiler: You Probably Already Have Them)

  • On most Linux systems, ps is part of procps (pre-installed).
  • pstree might need a quick install: sudo apt install pstree (Debian/Ubuntu) or sudo yum install psmisc (CentOS/RHEL).

Step 2: Core Usage

  • See all running processes (like ps aux):
    ps aux
  • Sort by memory hogs:
    ps aux --sort=-%mem | head
  • Show process tree for ALL users:
    pstree -p
  • Show process tree for a specific user:
    pstree username
  • Show tree for a specific PID:
    pstree 1234
  • Find a process by name:
    ps aux | grep nginx

Step 3: Practical Diagrams (ASCII Style)

root─┬─sshd───bash───pstree
     ├─cron
     └─nginx───nginx

This ASCII tree is what pstree outputs. Instantly shows you what’s running under whom!

Mini Glossary: Real-Talk Definitions

  • Process: A running instance of a program. Could be your app, a database, or even a sneaky script.
  • PID: Process ID – every process gets a unique number. Like a social security number, but for code.
  • PPID: Parent Process ID – who’s your daddy? (Which process spawned this one?)
  • Zombie: A dead process that hasn’t been “reaped” by its parent. Haunts your process list.
  • Daemon: Background service, often started at boot (like cron, sshd, etc.).
  • TTY: Terminal type. If blank, the process is running in the background.
  • UID: User ID. Tells you who owns the process.

Examples and Cases: The Good, The Bad, and The Ugly

Comic Metaphor Table: Who’s Who at the Process Party?

  • 🦸 The Hero: postgres – quietly running as a service, not hogging resources.
  • 🤖 The Robot: nginx master process, spawning worker processes – all behaving as expected.
  • 👻 The Ghost: [kworker/u8:2] – kernel process, nothing to worry about (unless it’s eating CPU).
  • 🤬 The Villain: python script.py – runaway script, 100% CPU, hundreds of child processes (caught by pstree).
  • 🧟 The Zombie: defunct – stuck in limbo, needs a parent to clean up.
  • 🦹 The Spy: minerd or cryptominer – shouldn’t even be at your party. Time to investigate!

Recommendation: Use ps aux --sort=-%cpu or pstree -p to find the troublemakers fast.

Beginner Mistakes, Myths, and Similar Tools

  • Myth: “ps only shows my processes.”
    Fact: Use ps aux for ALL processes, or ps -ef (SysV style).
  • Myth: “pstree is only for experts.”
    Fact: It’s actually easier to read than ps if you want to see relationships.
  • Mistake: Not using grep to filter output.
    Pro tip: ps aux | grep apache finds your Apache processes instantly.
  • Similar tools: top (real-time, but less readable for relationships), htop (interactive, colorful), pgrep (search by name), pkill (kill by name), systemctl status (for systemd services).

“Use This If…” Decision Tree

  Want to see which processes are running?
           ↓
        Use ps
           ↓
  Want to see the family tree (who spawned what)?
           ↓
        Use pstree
           ↓
  Want to watch resources in real-time?
           ↓
        Use top or htop
           ↓
  Need to find or kill by name?
           ↓
        Use pgrep / pkill

Still not sure? Start with ps aux | grep myapp and pstree -p. You’ll look and feel like a pro.

Fun Facts and Unconventional Uses

  • ps can output in JSON (with jq): ps -eo pid,ppid,cmd --no-headers | jq -R -s -c 'split("\n") | map(select(length > 0))'
  • pstree can highlight your user’s processes: pstree -u
  • You can use ps to count running containers: ps aux | grep docker-containerd | wc -l
  • Want to impress your team? Pipe ps aux into a script that emails you if your app isn’t running!
  • Pstree Easter Egg: Try pstree -a to see command-line args in the tree.

ps and pstree in Automation and Scripting

Both tools are script-friendly. Here’s a sample bash snippet to restart a service if it dies:

if ! ps aux | grep -v grep | grep -q myservice; then
  systemctl restart myservice
  echo "myservice was down, restarted at $(date)" >> /var/log/myservice_monitor.log
fi

Or, find and kill all zombie processes:

ps -el | awk '$2 == "Z" {print $4}' | xargs -r kill -9

Want to visualize process trees every hour? Run pstree -p > /var/log/pstree_hourly.log from cron.

Short Admin Story

Once had a colleague who swore by top for everything, but he always missed when cron jobs spawned hundreds of orphaned processes. After I introduced him to pstree, he became the office “process whisperer.” He could instantly spot which database backup script was leaving zombies, and our server uptime (and his coffee breaks) improved dramatically.

Conclusion – Wrap-up & Recommendations

Mastering ps and pstree isn’t just a “nice to have” – it’s a foundational skill for anyone running, managing, or securing a server. They’re fast, lightweight, and tell you exactly what’s happening under the hood. Use them to spot performance issues, fight off rogue processes, and automate your monitoring. If you’re setting up a new environment, whether that’s a VPS, dedicated server, or your own Docker jungle, these tools should be part of your daily kit.

So, next time you need to play server detective, reach for ps and pstree – and keep your process party under control. Happy debugging!

Ready to put your new skills to use? Order your next VPS or dedicated server at mangohost.net/vps or mangohost.net/dedicated and start exploring!


ps at procps-ng on Gitlab |
Official ps man page |
Official pstree man page



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked