?? Supercharge Your Linux Experience: Trending Linux Scripts for Daily Productivity ??
Kundan Antyakula
DevSecOps | AWS Certified (2x) | GitHub Certified (1x) | Kubernetes & Containerization | CI/CD & Infrastructure Automation | Driving Secure & Scalable DevOps Solutions
1. System Resource Monitoring Scripts
#!/bin/bash
echo "CPU Usage:"
top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4"%"}'
echo "Memory Usage:"
free -h
echo "Disk Usage:"
df -h
echo "I/O Activity:"
iostat -dx 1 5
2. Network Troubleshooting Script
#!/bin/bash
echo "Pinging Google DNS to check Internet connectivity..."
ping -c 4 8.8.8.8
echo "Testing DNS resolution for example.com..."
nslookup example.com
echo "Displaying network interfaces and IPs..."
ip addr show
echo "Checking open ports with netstat..."
netstat -tuln
3. Service Status Check Script
#!/bin/bash
services=("nginx" "mysql" "sshd")
for service in "${services[@]}"
do
echo "Checking $service status..."
systemctl is-active --quiet $service && echo "$service is running" || echo "$service is not running"
done
4. Disk Usage and Inode Monitoring Script
#!/bin/bash
echo "Checking disk usage..."
df -h
echo "Checking inode usage..."
df -i
5. Log Analysis Script
#!/bin/bash
echo "Checking for recent errors in syslog..."
grep -i "error" /var/log/syslog | tail -n 20
echo "Checking for recent errors in dmesg..."
dmesg | grep -i "error" | tail -n 20
6. Process Hang Detection Script
#!/bin/bash
echo "Processes using high CPU:"
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head
echo "Processes using high memory:"
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
7. File System Integrity Check Script
#!/bin/bash
echo "Checking all file systems for errors..."
for fs in $(lsblk -f | grep -v "loop" | awk '{print $1}')
do
sudo fsck -n /dev/$fs
done
8. Package Version Check Script
#!/bin/bash
echo "Checking for package updates..."
if [ -f /etc/debian_version ]; then
sudo apt update && sudo apt list --upgradable
elif [ -f /etc/redhat-release ]; then
sudo yum check-update
else
echo "Unsupported distribution"
fi
9. SSH Troubleshooting Script
#!/bin/bash
echo "Testing SSH connection to localhost..."
ssh -v localhost exit
echo "Checking SSHD configuration..."
sudo sshd -t
echo "Checking SSH log for recent errors..."
sudo grep sshd /var/log/auth.log | tail -n 20
10. Automated Troubleshooting Report Script
#!/bin/bash
REPORT_FILE="troubleshooting_report.txt"
echo "Collecting system information..." > $REPORT_FILE
echo "Uptime:" >> $REPORT_FILE
uptime >> $REPORT_FILE
echo "CPU Info:" >> $REPORT_FILE
lscpu >> $REPORT_FILE
echo "Memory Info:" >> $REPORT_FILE
free -h >> $REPORT_FILE
echo "Disk Usage:" >> $REPORT_FILE
df -h >> $REPORT_FILE
echo "Network Configuration:" >> $REPORT_FILE
ip addr show >> $REPORT_FILE
echo "Top Processes:" >> $REPORT_FILE
ps aux --sort=-%cpu | head -10 >> $REPORT_FILE
echo "Log Errors:" >> $REPORT_FILE
grep -i "error" /var/log/syslog | tail -n 20 >> $REPORT_FILE
echo "System Report saved to $REPORT_FILE"
Conclusion
These scripts can be customized to suit your environment and troubleshooting needs. It's often helpful to automate tasks like these, especially in larger environments where manual checks can become time-consuming. Keep in mind that you should test scripts on a non-production system first and consider the security implications (e.g., checking logs for sensitive information).