|
1 | 1 | #!/usr/bin/env bash
|
| 2 | +set -euo pipefail |
2 | 3 |
|
3 | 4 | # Script Name: orphans.sh
|
4 |
| -# Description: This script displays processes that might be orphans, i.e. processes that have no parent process. |
5 |
| -# Usage: `chmod +x orphans.sh && ./orphans.sh` |
6 |
| -# Example: `./orphans.sh` displays a list of processes that might be orphans. |
| 5 | +# Description: This script displays processes that might be orphans, |
| 6 | +# i.e. processes whose parent process is not running. |
| 7 | +# Usage: chmod +x orphans.sh && ./orphans.sh |
| 8 | +# Example: ./orphans.sh displays a list of processes that might be orphans. |
7 | 9 |
|
8 |
| -# Temporary files |
| 10 | +# Create temporary files for process data. |
9 | 11 | TMP_FILE=$(mktemp /tmp/processes.XXXXXX)
|
10 |
| -PPID_TMP_FILE=$(mktemp /tmp/ppid.XXXXXX) |
| 12 | +PIDS_TMP_FILE=$(mktemp /tmp/pids.XXXXXX) |
11 | 13 |
|
12 |
| -# Cleanup function |
| 14 | +# Cleanup function to remove temporary files. |
13 | 15 | cleanup() {
|
14 |
| - rm -f "$TMP_FILE" |
15 |
| - rm -f "$PPID_TMP_FILE" |
| 16 | + rm -f "$TMP_FILE" "$PIDS_TMP_FILE" |
16 | 17 | }
|
17 | 18 |
|
18 |
| -# Error handling function |
| 19 | +# Error handling function. |
19 | 20 | error_exit() {
|
20 |
| - echo "$1" 1>&2 |
| 21 | + echo "$1" >&2 |
21 | 22 | cleanup
|
22 | 23 | exit 1
|
23 | 24 | }
|
24 | 25 |
|
25 |
| -# Function to check for orphan processes |
| 26 | +# Function to check for orphan processes. |
26 | 27 | check_orphans() {
|
27 |
| - # Get a list of all processes' PID and PPID |
28 |
| - ps -eo ppid,pid | sed 1d > "$TMP_FILE" |
| 28 | + # Get a list of all processes' PPID and PID (without the header). |
| 29 | + ps -eo ppid,pid --no-headers > "$TMP_FILE" |
29 | 30 |
|
30 |
| - # Create a list of all parent process IDs |
31 |
| - awk '{print $1}' "$TMP_FILE" | sort -u > "$PPID_TMP_FILE" |
| 31 | + # Create a list of all running process IDs (PIDs) from the second column. |
| 32 | + awk '{print $2}' "$TMP_FILE" | sort -u > "$PIDS_TMP_FILE" |
32 | 33 |
|
33 |
| - # Check if each process has a parent process |
| 34 | + # Check if each process has a parent process running. |
34 | 35 | while read -r ppid pid; do
|
35 |
| - if ! grep -q -w "^$ppid$" "$PPID_TMP_FILE"; then |
36 |
| - echo "Process $pid might be an orphan" |
| 36 | + # Skip kernel or system processes with parent PID 0. |
| 37 | + if [ "$ppid" -eq 0 ]; then |
| 38 | + continue |
| 39 | + fi |
| 40 | + |
| 41 | + # If the parent's PID is not in the list of running PIDs, |
| 42 | + # then the process might be orphaned. |
| 43 | + if ! grep -q -w "^$ppid$" "$PIDS_TMP_FILE"; then |
| 44 | + echo "Process $pid might be an orphan (parent PID: $ppid not found)" |
37 | 45 | fi
|
38 | 46 | done < "$TMP_FILE"
|
39 | 47 | }
|
40 | 48 |
|
41 |
| -# Check if the temp files were created |
42 |
| -if [[ ! -e "$TMP_FILE" || ! -e "$PPID_TMP_FILE" ]]; then |
| 49 | +# Ensure that temporary files were created. |
| 50 | +if [[ ! -e "$TMP_FILE" || ! -e "$PIDS_TMP_FILE" ]]; then |
43 | 51 | error_exit "Failed to create temporary file. Make sure you have the right permissions."
|
44 | 52 | fi
|
45 | 53 |
|
46 |
| -# Set a trap for cleanup |
| 54 | +# Set a trap to cleanup temporary files on exit. |
47 | 55 | trap cleanup EXIT
|
48 | 56 |
|
49 |
| -# Run the main function |
| 57 | +# Run the orphan-check function. |
50 | 58 | check_orphans
|
0 commit comments