Script to keep only the latest 20 files in the current directory.
cleanup.sh
#!/bin/bash# Script to keep only the latest 20 files in the current directorytotal_files=$(ls-1|wc-l)files_to_delete=$((total_files-20))if["$files_to_delete"-gt0];thenecho"Deleting $files_to_delete oldest files..."ls-1t|tail-n"$files_to_delete"|xargs-d'\n'rm-rfelseecho"Less than or equal to 20 files, nothing to delete."fi
✅ 2. Service Health Check & Restart (check_services.sh)
Monitor and restart stopped services, and send email notification if needed.
check_services.sh
#!/bin/bash# Monitor and restart stopped services, send email notification if neededservices="serv1 serv2 serv3"log_file="/tmp/stopped_services.log">"$log_file"# Clear log fileforservicein$services;doif!pgrep-f"$service">/dev/null;thenecho"Service $service is not running.">>"$log_file"echo"Attempting to restart $service...">>"$log_file"sudoservice"$service"restart>>"$log_file"2>&1echo"Service $service was down and has been restarted. Please verify."\|mail-s"Alert: $service service was stopped"-c[email protected][email protected]fidone
✅ 3. Disk Space Monitor (memorycheck.sh)
Check if current disk usage exceeds threshold and send alert.