Back to Blog

Zsh Process Management — Control, Monitor & Signal Processes #15

Sandy LaneSandy Lane

Video: Zsh Process Management — Control, Monitor & Signal Processes #15 by Taught by Celeste AI - AI Coding Coach

Watch full page →

Zsh Process Management — Control, Monitor & Signal Processes

Mastering process management in Zsh is essential for efficient shell usage. This guide demonstrates how to view running processes with ps and pgrep, control background jobs, handle signals for cleanup using trap, and keep processes running with nohup and killall.

Code

# View all running processes with detailed info
ps aux

# Search for processes by name (e.g., "zsh")
pgrep -fl zsh

# Start a long-running job in the background
sleep 300 &

# List current background jobs
jobs

# Bring a background job to the foreground (job number 1)
fg %1

# Stop a foreground job with Ctrl+Z, then resume it in the background
bg %1

# Use trap to handle signals (e.g., cleanup on Ctrl+C)
trap 'echo "Cleaning up..."; exit' INT

# Example: Run a command that traps SIGINT
while true; do
  echo "Running... Press Ctrl+C to stop"
  sleep 1
done

# Run a command immune to hangups with nohup
nohup long_running_script.sh &

# Kill all processes by name (e.g., all "sleep" commands)
killall sleep

Key Points

  • ps and pgrep help you find and inspect running processes efficiently.
  • Job control commands like jobs, fg, and bg manage background and foreground tasks interactively.
  • trap allows your script to catch signals such as SIGINT for graceful termination and cleanup.
  • nohup lets processes continue running even after you log out or close the terminal.
  • killall is a convenient way to terminate all processes matching a given name.