Skip to content

Commit 5831817

Browse files
authoredFeb 2, 2025··
Update orphans.sh
1 parent 773b864 commit 5831817

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed
 

Diff for: ‎src/orphans.sh

+30-22
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,58 @@
11
#!/usr/bin/env bash
2+
set -euo pipefail
23

34
# 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.
79

8-
# Temporary files
10+
# Create temporary files for process data.
911
TMP_FILE=$(mktemp /tmp/processes.XXXXXX)
10-
PPID_TMP_FILE=$(mktemp /tmp/ppid.XXXXXX)
12+
PIDS_TMP_FILE=$(mktemp /tmp/pids.XXXXXX)
1113

12-
# Cleanup function
14+
# Cleanup function to remove temporary files.
1315
cleanup() {
14-
rm -f "$TMP_FILE"
15-
rm -f "$PPID_TMP_FILE"
16+
rm -f "$TMP_FILE" "$PIDS_TMP_FILE"
1617
}
1718

18-
# Error handling function
19+
# Error handling function.
1920
error_exit() {
20-
echo "$1" 1>&2
21+
echo "$1" >&2
2122
cleanup
2223
exit 1
2324
}
2425

25-
# Function to check for orphan processes
26+
# Function to check for orphan processes.
2627
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"
2930

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"
3233

33-
# Check if each process has a parent process
34+
# Check if each process has a parent process running.
3435
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)"
3745
fi
3846
done < "$TMP_FILE"
3947
}
4048

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
4351
error_exit "Failed to create temporary file. Make sure you have the right permissions."
4452
fi
4553

46-
# Set a trap for cleanup
54+
# Set a trap to cleanup temporary files on exit.
4755
trap cleanup EXIT
4856

49-
# Run the main function
57+
# Run the orphan-check function.
5058
check_orphans

0 commit comments

Comments
 (0)
Please sign in to comment.