Skip to content

Instantly share code, notes, and snippets.

@devsetgo
Last active October 3, 2025 23:28
Show Gist options
  • Select an option

  • Save devsetgo/e0fa53f79506dd9e7a972bb78fa5c20b to your computer and use it in GitHub Desktop.

Select an option

Save devsetgo/e0fa53f79506dd9e7a972bb78fa5c20b to your computer and use it in GitHub Desktop.
bashrc custom server commands for login
# System monitoring and maintenance functions
# These functions provide comprehensive system status information
# and help identify potential issues with the system
run_system_checks() {
echo "=== SYSTEM STATUS CHECKS ==="
echo "Running routine system checks that don't require elevated privileges..."
echo ""
# Docker container status - shows running containers and their health
echo "Displaying Docker containers status..."
echo "This shows all running containers, their status, ports, and resource usage"
docker ps
echo ""
# Disk usage - critical for preventing out-of-space issues
echo "Checking disk usage..."
echo "Monitors available disk space on all mounted filesystems (-h = human readable)"
df -h
echo ""
# Memory usage - helps identify memory leaks or high usage
echo "Checking memory usage..."
echo "Shows total, used, free, and available memory (-h = human readable)"
free -h
echo ""
# Network connections - shows active ports and connections
echo "Listing active network connections..."
echo "Shows listening ports and established connections (-t=TCP, -u=UDP, -l=listening, -n=numeric)"
netstat -tuln
echo ""
# Failed services - identifies system service issues
echo "Checking for failed systemd services..."
echo "Lists any systemd services that have failed - helps identify system problems"
systemctl --failed
echo ""
# System uptime - shows how long system has been running
echo "Displaying system uptime..."
echo "Shows system boot time, uptime, users logged in, and load averages"
uptime
echo ""
}
run_sudo_checks() {
echo "=== SYSTEM LOGS (requires sudo) ==="
echo "Running system log checks that require administrative privileges..."
echo ""
# System logs - shows recent system events and errors
echo "Showing system logs (last 10 lines)..."
echo "Displays recent system events from /var/log/syslog - useful for troubleshooting"
echo "This requires sudo to access system log files"
sudo tail -n 10 /var/log/syslog
echo ""
}
check_uptime_and_restart() {
echo "=== UPTIME CHECK ==="
# Get uptime in days
uptime_days=$(awk '{print int($1/86400)}' /proc/uptime)
echo "Current system uptime: $uptime_days days"
if [ "$uptime_days" -ge 25 ]; then
echo ""
echo "⚠️ WARNING: System has been running for $uptime_days days (25+ days)"
echo "Long uptimes can lead to memory fragmentation and potential issues."
echo "It's recommended to restart the system for optimal performance."
echo ""
echo "Do you want to restart the server now? (y/n)"
read restart_answer
if [ "$restart_answer" != "${restart_answer#[Yy]}" ]; then
echo "Scheduling system restart in 1 minute..."
echo "You can cancel with: sudo shutdown -c"
sudo shutdown -r +1 "System restart scheduled via .bashrc maintenance check"
else
echo "Restart skipped. Consider restarting during your next maintenance window."
fi
else
echo "✅ System uptime is acceptable ($uptime_days days < 25 days threshold)"
fi
echo ""
}
# Prompt for system updates
echo "Do you want to run system updates? (y/n)"
read answer
if [ "$answer" != "${answer#[Yy]}" ] ;then
echo "Running system updates..."
sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y
echo ""
fi
# Run non-sudo system checks
run_system_checks
# Prompt for sudo-required checks
echo "Do you want to run system log checks? (requires sudo) (y/n)"
read sudo_answer
if [ "$sudo_answer" != "${sudo_answer#[Yy]}" ] ;then
run_sudo_checks
fi
# Check system uptime and offer restart if needed
check_uptime_and_restart
# Function to check recent login activity
check_recent_logins() {
echo "=== RECENT LOGIN ACTIVITY ==="
echo "Showing login activity from the last 10 days..."
echo "This displays user logins, logouts, and system reboots"
echo ""
# Get current date for comparison (10 days ago)
cutoff_date=$(date -d '10 days ago' '+%Y-%m-%d')
echo "Showing logins since: $cutoff_date"
echo ""
# Show recent logins using a simpler approach (oldest to newest)
echo "Recent logins (last 10 days - oldest to newest):"
# Create a temporary file to store recent logins
temp_file=$(mktemp)
last -n 50 | while read line; do
if [[ $line =~ ^(mike|root|[a-zA-Z][a-zA-Z0-9_-]*)[[:space:]] ]]; then
# Extract date from the line (format: Day Mon DD HH:MM)
if [[ $line =~ [[:space:]]([A-Z][a-z][a-z])[[:space:]]+([0-9]{1,2})[[:space:]] ]]; then
month_name="${BASH_REMATCH[1]}"
day="${BASH_REMATCH[2]}"
# Convert month name to number
case $month_name in
Jan) month="01" ;;
Feb) month="02" ;;
Mar) month="03" ;;
Apr) month="04" ;;
May) month="05" ;;
Jun) month="06" ;;
Jul) month="07" ;;
Aug) month="08" ;;
Sep) month="09" ;;
Oct) month="10" ;;
Nov) month="11" ;;
Dec) month="12" ;;
*) continue ;;
esac
# Format the date for comparison
current_year=$(date '+%Y')
# Convert day to decimal integer first, then zero-pad
day_int=$((10#$day))
day_padded=$(printf "%02d" $day_int)
login_date=$(printf "%04d-%s-%s" $current_year $month $day_padded)
# Compare dates (only show if within last 10 days)
if [[ $login_date > $cutoff_date ]] || [[ $login_date == $cutoff_date ]]; then
echo "$line" >> "$temp_file"
fi
fi
elif [[ $line =~ ^reboot ]]; then
# Also show reboots if they're recent
if [[ $line =~ [[:space:]]([A-Z][a-z][a-z])[[:space:]]+([0-9]{1,2})[[:space:]] ]]; then
month_name="${BASH_REMATCH[1]}"
day="${BASH_REMATCH[2]}"
case $month_name in
Jan) month="01" ;;
Feb) month="02" ;;
Mar) month="03" ;;
Apr) month="04" ;;
May) month="05" ;;
Jun) month="06" ;;
Jul) month="07" ;;
Aug) month="08" ;;
Sep) month="09" ;;
Oct) month="10" ;;
Nov) month="11" ;;
Dec) month="12" ;;
*) continue ;;
esac
current_year=$(date '+%Y')
# Convert day to decimal integer first, then zero-pad
day_int=$((10#$day))
day_padded=$(printf "%02d" $day_int)
reboot_date=$(printf "%04d-%s-%s" $current_year $month $day_padded)
if [[ $reboot_date > $cutoff_date ]] || [[ $reboot_date == $cutoff_date ]]; then
echo "$line" >> "$temp_file"
fi
fi
fi
done
# Display the logins in reverse order (oldest to newest) and clean up
if [[ -s "$temp_file" ]]; then
tac "$temp_file" | sed 's/^/ /'
else
echo " No recent login activity found in the last 10 days"
fi
rm -f "$temp_file"
echo ""
# Show unique users who logged in recently (simpler version)
echo "Unique users with recent login activity:"
last -n 50 | grep -E "^(mike|root|[a-zA-Z][a-zA-Z0-9_-]*)[[:space:]]" | \
while read line; do
if [[ $line =~ [[:space:]]([A-Z][a-z][a-z])[[:space:]]+([0-9]{1,2})[[:space:]] ]]; then
month_name="${BASH_REMATCH[1]}"
day="${BASH_REMATCH[2]}"
case $month_name in
Jan) month="01" ;;
Feb) month="02" ;;
Mar) month="03" ;;
Apr) month="04" ;;
May) month="05" ;;
Jun) month="06" ;;
Jul) month="07" ;;
Aug) month="08" ;;
Sep) month="09" ;;
Oct) month="10" ;;
Nov) month="11" ;;
Dec) month="12" ;;
*) continue ;;
esac
current_year=$(date '+%Y')
# Convert day to decimal integer first, then zero-pad
day_int=$((10#$day))
day_padded=$(printf "%02d" $day_int)
login_date=$(printf "%04d-%s-%s" $current_year $month $day_padded)
if [[ $login_date > $cutoff_date ]] || [[ $login_date == $cutoff_date ]]; then
user=$(echo $line | awk '{print $1}')
echo " $user"
fi
fi
done | sort | uniq
echo ""
# Show failed login attempts if available
echo "Recent failed login attempts (if any):"
if command -v lastb >/dev/null 2>&1; then
sudo lastb -n 20 | head -10 2>/dev/null || echo " No failed login attempts or insufficient permissions"
else
echo " lastb command not available"
fi
echo ""
}
# Check recent login activity
check_recent_logins
@devsetgo
Copy link
Author

devsetgo commented Aug 1, 2025

These are my custom .bashrc commands. I this simplifies system checks and updates after I login. Also makes sure I keep my system up to date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment