Skip to content

Instantly share code, notes, and snippets.

@greenido
Created August 2, 2023 22:52
Show Gist options
  • Save greenido/32bfcf44eaf8b39ca4f31527abf464a7 to your computer and use it in GitHub Desktop.
Save greenido/32bfcf44eaf8b39ca4f31527abf464a7 to your computer and use it in GitHub Desktop.
A shell script to automate system monitoring, maintenance and email you the results
#!/bin/bash
# System Maintenance Script
# Create log file
log=/var/log/maintenance.log
# Print start status
echo "$(date) - Starting system maintenance" >> $log
# Update package lists
apt update >> $log 2>&1
# Upgrade packages
apt upgrade -y >> $log 2>&1
# Clear package cache
apt autoremove --purge -y >> $log 2>&1
# Check disk usage
diskusage=( $(df -h | grep -v "Filesystem" | awk '{print $5}') )
echo "$(date) - Disk usage:" >> $log
printf '%s\n' "${diskusage[@]}" >> $log
# List open files
openfiles=( $(lsof | wc -l) )
echo "$(date) - Open files: $openfiles" >> $log
# Email log file
mail -s "System Maintenance Log" admin@example.com < $log
echo "$(date) - Maintenance complete" >> $log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment