mauvehed's dotfiles for personal and work environments
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(executable_check_uptime.sh): add macOS support for uptime check

Add detection for macOS and calculate uptime in days using `sysctl`
and `date` commands. This change allows the script to function on
both Linux and macOS systems, increasing its versatility. Additionally,
add a check for unsupported operating systems to prevent errors.
The reboot-required check is now Linux-specific, ensuring compatibility
across different environments.

+16 -5
+16 -5
dot_local/bin/executable_check_uptime.sh
··· 1 1 #!/bin/bash 2 2 3 - # Get the current uptime in days 4 - UPTIME_DAYS=$(awk '{print int($1 / 86400)}' /proc/uptime) 5 - 6 3 # Set the threshold for uptime warning 7 4 UPTIME_THRESHOLD=60 8 5 6 + # Detect OS and get uptime in days 7 + if [[ "$OSTYPE" == "linux-gnu"* ]]; then 8 + # Get uptime in days on Linux 9 + UPTIME_DAYS=$(awk '{print int($1 / 86400)}' /proc/uptime) 10 + elif [[ "$OSTYPE" == "darwin"* ]]; then 11 + # Get uptime in days on macOS 12 + UPTIME_SECONDS=$(sysctl -n kern.boottime | awk '{print $4}' | sed 's/,//') 13 + CURRENT_TIME=$(date +%s) 14 + UPTIME_DAYS=$(( (CURRENT_TIME - UPTIME_SECONDS) / 86400 )) 15 + else 16 + echo "Unsupported OS" 17 + exit 1 18 + fi 19 + 9 20 # Check if the uptime is greater than the threshold 10 21 if [ "$UPTIME_DAYS" -gt "$UPTIME_THRESHOLD" ]; then 11 22 # Set the color to red for the warning message ··· 16 27 echo -e "${RED}Warning: System uptime is ${UPTIME_DAYS} days, which is above ${UPTIME_THRESHOLD} days threshold.${NC}" 17 28 fi 18 29 19 - # Check if a reboot is required 20 - if [ -f /var/run/reboot-required ]; then 30 + # Check if a reboot is required (Linux-only) 31 + if [[ "$OSTYPE" == "linux-gnu"* && -f /var/run/reboot-required ]]; then 21 32 # Display a reboot warning 22 33 echo -e "${RED}Warning: A system reboot is required for patching.${NC}" 23 34 fi