DEV Community

Cover image for The Linux Process Party: How to Kill, Fork, and Keep It All Running Smoothly
Sandheep Kumar Patro
Sandheep Kumar Patro

Posted on

The Linux Process Party: How to Kill, Fork, and Keep It All Running Smoothly

Mastering Linux Processes: The Art of Commanding the Chaos ✨

Let’s face it: managing processes in Linux can feel like herding cats. But with the right knowledge and a dash of humor, you can turn chaos into order. This blog will present you the ins and outs of Linux process management so you can command your terminal like a boss. Let’s dive in! 🚀


1. Foreground vs. Background Processes: The Tale of Two Worlds

a) Foreground Process

A foreground process is like your clingy friend who demands all your attention. When you run a command, your terminal is tied up until the process finishes or you rudely interrupt it.

Example:

sleep 10
Enter fullscreen mode Exit fullscreen mode

Your terminal will be unresponsive for 10 seconds—perfect time for a coffee break. ☕

b) Background Process

Want to multitask? Run your process in the background by appending & to the command. This is like saying, “You do you, and I’ll check on you later.”

Example:

sleep 10 &
Enter fullscreen mode Exit fullscreen mode

Output:

[1] 12345
Enter fullscreen mode Exit fullscreen mode
  • [1]: Job ID (like a tag to keep track of your “tasks”)
  • 12345: Process ID (PID, the process’s unique number)

2. Suspending and Resuming Processes: The Power of Pause

Suspend (Pause) a Process

Press Ctrl+Z while a process is running. This suspends it and puts it in the background—like hitting the snooze button.

Resume the Process

  • Bring it back to the foreground:
  fg %job_id
Enter fullscreen mode Exit fullscreen mode
  • Resume it in the background:
  bg %job_id
Enter fullscreen mode Exit fullscreen mode

List Jobs

Need to see what’s happening? Use:

jobs
Enter fullscreen mode Exit fullscreen mode

Example output:

[1]-  Stopped    sleep 10
[2]+  Running    ping google.com &
Enter fullscreen mode Exit fullscreen mode

3. Detaching a Process: Let It Run Wild

a) disown

You can cut the strings attaching a background process to your terminal with disown. The process will keep running, even if you close the terminal.

Example:

sleep 100 &
disown
Enter fullscreen mode Exit fullscreen mode

b) nohup

Want your process to be completely unbothered by terminal closures? Use nohup (short for “no hangup”):

nohup your_command &
Enter fullscreen mode Exit fullscreen mode

Output is redirected to nohup.out by default. It’s like giving your process noise-canceling headphones.

c) setsid

Another way to detach a process:

setsid your_command
Enter fullscreen mode Exit fullscreen mode

4. Viewing Running Processes: Who’s Doing What?

a) ps

Think of ps as your backstage pass to see the current lineup of processes:

ps aux        # Detailed list of all processes
ps -ef        # Another detailed format
Enter fullscreen mode Exit fullscreen mode

To find a specific process:

ps aux | grep your_process_name
Enter fullscreen mode Exit fullscreen mode

b) topand htop

  • top: A live, no-frills view of system processes.
  top
Enter fullscreen mode Exit fullscreen mode
  • htop: The cooler, more user-friendly cousin of top. Navigate with arrow keys like a pro.

c) pgrep

Quickly find processes by name:

pgrep -fl your_process_name
Enter fullscreen mode Exit fullscreen mode

5. Managing Processes: Playing the Terminator

a) Kill a Process

When a process misbehaves, you need to don the shades and terminate it:

kill <pid>
Enter fullscreen mode Exit fullscreen mode
  • Signals:

    • -15 (SIGTERM): Politely ask the process to stop.
    • -9 (SIGKILL): Forcefully stop it—no pleasantries.
    kill -9 <pid>
    

b) Stop or Pause a Process

Send SIGSTOP to pause a process:

kill -STOP <pid>
Enter fullscreen mode Exit fullscreen mode

Resume it with SIGCONT:

kill -CONT <pid>
Enter fullscreen mode Exit fullscreen mode

6. Process Hierarchies: Meet the Family Tree

Processes have parents and children—it’s a family affair. View the family tree with:

pstree
Enter fullscreen mode Exit fullscreen mode

For example, running bash spawns child processes like ping or sleep. Every child needs a parent—unless it’s an orphan (more on that later).


7. Advanced Tools for Managing Processes

a) screen

Run processes in detachable terminal sessions.

  • Start a new screen session:
  screen
Enter fullscreen mode Exit fullscreen mode
  • Detach with Ctrl+A, D, and reattach:
  screen -r
Enter fullscreen mode Exit fullscreen mode

b) strace

Trace the system calls and signals of a process:

strace -p <pid>
Enter fullscreen mode Exit fullscreen mode

c) nice

Control process priority. “Nice” processes don’t hog resources.

  • Start a process with a lower priority:
  nice -n 10 your_command
Enter fullscreen mode Exit fullscreen mode
  • Change the priority of a running process:
  renice 5 -p <pid>
Enter fullscreen mode Exit fullscreen mode

8. Zombie and Orphan Processes: Undead and Abandoned

Zombie Processes

These are processes that have completed but still linger in the process table—like ghosts. To find them:

ps aux | grep Z
Enter fullscreen mode Exit fullscreen mode

Solution? Kill their parent process. Bye, zombies!

Orphan Processes

When a parent process dies, the orphaned children are adopted by the init process (PID 1), which becomes their new guardian. Unlike zombies, orphans are perfectly functional and continue to run happily under their new parent.


9. Automating Process Management: Be the Puppet Master

Write a Script

Automate the boring stuff with shell scripts:

#!/bin/bash
my_process="your_process_name"
pid=$(pgrep $my_process)

if [ -z "$pid" ]; then
  echo "$my_process is not running."
else
  echo "$my_process is running with PID $pid."
  kill -9 $pid
fi
Enter fullscreen mode Exit fullscreen mode

Use cron for Scheduled Processes

Run processes on autopilot with cron:

crontab -e
Enter fullscreen mode Exit fullscreen mode

Example entry:

0 2 * * * /path/to/your_script.sh
Enter fullscreen mode Exit fullscreen mode

10. Signals Cheat Sheet: Speak the Process’ Language

Signal Number Description Example Command
SIGINT 2 Interrupt (Ctrl+C) kill -2 <pid>
SIGTERM 15 Gracefully terminate kill -15 <pid>
SIGKILL 9 Forcefully terminate kill -9 <pid>
SIGHUP 1 Hangup (reload configs) kill -1 <pid>
SIGSTOP 19 Pause process kill -STOP <pid>
SIGCONT 18 Resume paused process kill -CONT <pid>

Final Thoughts

Managing processes in Linux isn’t just a skill—it’s an art. From suspending and resuming to killing zombies, every command gives you more control over your system. Now go forth and master the terminal, one process at a time. Happy herding! 🐱‍❤️

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free