-
Notifications
You must be signed in to change notification settings - Fork 0
/
health-checker.sh
executable file
·68 lines (58 loc) · 1.71 KB
/
health-checker.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
# Temporary files for storing results
temp_success=$(mktemp)
temp_fail=$(mktemp)
temp_redirect=$(mktemp)
# Function to check a single URL
check_url() {
url="$1"
response=$(curl -o /dev/null -s -w "%{http_code}" -m 10 "$url")
if [ "$response" -ge 200 ] && [ "$response" -lt 300 ]; then
echo "$url: OK ($response)"
echo "$url: OK ($response)" >> "$temp_success"
elif [ "$response" -ge 300 ] && [ "$response" -lt 400 ]; then
echo "$url: REDIRECT ($response)"
echo "$url: REDIRECT ($response)" >> "$temp_redirect"
else
echo "$url: FAIL ($response)"
echo "$url: FAIL ($response)" >> "$temp_fail"
fi
}
# Check if a file is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 <file_with_urls>"
exit 1
fi
# Check if the file exists
if [ ! -f "$1" ]; then
echo "Error: File not found!"
exit 1
fi
# Set maximum number of parallel processes
MAX_PROCS=10
# Read URLs from file and check each one
while IFS= read -r url || [ -n "$url" ]; do
if [ -n "$url" ]; then
# Run check_url in background
check_url "$url" &
# Limit the number of parallel processes
while [ $(jobs -p | wc -l) -ge $MAX_PROCS ]; do
sleep 0.1
done
fi
done < "$1"
# Wait for all background processes to finish
wait
# Print results
echo -e "\n--- RESULTS ---"
echo "Successful URLs:"
cat "$temp_success"
echo -e "\nRedirected URLs:"
cat "$temp_redirect"
echo -e "\nFailed URLs:"
cat "$temp_fail"
echo -e "\nTotal Successful: $(wc -l < "$temp_success")"
echo "Total Redirected: $(wc -l < "$temp_redirect")"
echo "Total Failed: $(wc -l < "$temp_fail")"
# Clean up temporary files
rm "$temp_success" "$temp_fail" "$temp_redirect"