Chapter 5: Working with Processes and System Resources¶
Learning Objectives¶
By the end of this chapter, you will be able to:
- Monitor running processes and system usage
- Manage background and foreground jobs in Bash
- Use
ps,top,htop,kill,nice, andrenice - Automate long-running tasks
- Understand process IDs (PIDs) and signals
- Use macOS-specific tools like
Activity Monitor,launchctl, andpmset
Introduction: Why Processes Matter¶
When you run a Bash script, launch an app, or execute a command, you're starting a process. Understanding how to inspect, manage, and influence those processes is essential for automation, performance tuning, and security auditing on macOS.
Process management is critical for security engineers who need to monitor suspicious activity, stop malware, or audit system usage. For administrators, process knowledge enables you to troubleshoot performance issues, manage system resources, and schedule automated tasks effectively. This chapter covers the fundamental tools for process management on macOS, setting you up for advanced topics like launchd (covered in Chapter 10) and system monitoring (discussed in Chapter 18).
5.1 Viewing Running Processes¶
View system-wide processes¶
View your own processes¶
Real-time monitoring¶
Or install and use htop (more user-friendly):
5.2 Process IDs and Signals¶
Each running process has a PID. Use it to send signals:
List signals:
Send a signal by name:
5.3 Managing Foreground and Background Jobs¶
Run in background¶
View background jobs¶
Bring job to foreground¶
Stop a job¶
Kill a background job¶
5.4 Prioritizing with nice and renice¶
Run a process with lower priority:
Change priority of a running process:
Lower values = higher priority (0 is default).
5.5 Automating Long-Running Tasks¶
Use nohup to detach from terminal:
Use cron to schedule jobs:
Example:
Note: Replace
/Users/sammy/scripts/backup.shwith your actual script path. Consider using$HOME/scripts/backup.shor an absolute path like/usr/local/bin/backup.shfor system-wide scripts.
Use launchd for macOS-native task scheduling (discussed in later chapters).
5.6 Monitoring System Usage¶
Check CPU usage:
Check memory:
Disk usage:
Free disk space:
Battery and power status:
5.7 macOS-Specific Process Tools¶
Activity Monitor: GUI process viewerlaunchctl: manage background daemonspmset: control power settingsspindump,sample,fs_usage: diagnostic tools
Example: List user agents loaded by launchd:
Chapter 5 Exercise¶
Write a script watch_process.sh that:
- Accepts a process name
- Checks if it's running
- If not, logs a warning to a file
Hint:
#!/bin/bash
process="$1"
if ! pgrep -x "$process" > /dev/null; then
echo "[$(date)] $process is not running" >> "$HOME/process_warnings.log"
fi
macOS Scripting Tips¶
- Use
pgrepandpkillfor process name filtering - Use
nohupanddisownto prevent processes from dying with the terminal - Use
launchctl bootoutandlaunchctl bootinto disable/enable services - Monitor app hangs with
sample:sample [pid] -file output.txt - Use
spindumpto identify resource-intensive processes - Check battery status before running intensive scripts with
pmset -g batt