- In Middleware & DevOps automation, file handling is everywhere — reading logs, writing reports, rotating backups, or watching live output.
- Let's learn how to read, write, append, and monitor files in shell scripts with real-world Middleware examples.
π Table of Contents
π 1️⃣ Reading Files
Example 1: Read File Line by Line
#!/bin/bash
filename="/opt/scripts/serverlist.txt"
while read -r line; do
echo "Processing server: $line"
done < "$filename"
π¬ Output:
Processing server: app1Processing server: app2
Processing server: app3
Example 2: Display Tomcat Log Errors
#!/bin/bash
grep "ERROR" /opt/tomcat/logs/catalina.out
π¬ Output:
ERROR [localhost-startStop-1] ... Application failed to startERROR [main] ... Database connection refused
π‘ Tip: Use
grep -i error to ignore case.✍️ 2️⃣ Writing & Appending Files
Example 3: Write Output to File
#!/bin/bash
echo "Backup started at $(date)" > /opt/scripts/backup.log
echo "Compressing files..." >> /opt/scripts/backup.log
tar -czf /tmp/backup.tar.gz /opt/data && echo "Backup successful" >> /opt/scripts/backup.log
π¬ Output File Content:
Backup started at Sat Nov 1 02:00:00 IST 2025Compressing files...
Backup successful
Example 4: Append Deployment Status
#!/bin/bash
env=$1
status=$2
echo "$(date) - $env deployment $status" >> /opt/scripts/deploy_history.log
π¬ Output File Content:
Sat Nov 1 02:15:00 IST 2025 - dev deployment SUCCESSSat Nov 1 02:17:00 IST 2025 - prod deployment FAILED
Example 5: Log WebSphere Cleanup Activity
#!/bin/bash
logfile="/opt/IBM/WebSphere/AppServer/logs/cleanup.log"
echo "$(date): Cleaning old logs..." >> $logfile
find /opt/IBM/WebSphere/AppServer/logs -type f -mtime +7 -delete && echo "✅ Logs cleaned" >> $logfile
π¬ Output File:
Sat Nov 1 02:30:00 IST 2025: Cleaning old logs...✅ Logs cleaned
π 3️⃣ Monitoring Log Files
Example 6: Live Tail of Tomcat Logs
#!/bin/bash
echo "Monitoring Tomcat logs..."
tail -f /opt/tomcat/logs/catalina.out
π¬ Output:
Monitoring Tomcat logs...[INFO] Server startup completed in 2200 ms
[WARN] Deprecated API in use
[ERROR] Connection timeout to DB
Example 7: Auto-restart Jenkins on Log Failure
#!/bin/bash
tail -Fn0 /var/log/jenkins/jenkins.log | \
while read line; do
echo "$line" | grep "ERROR" &>/dev/null
if [ $? = 0 ]; then
echo "π¨ Error detected, restarting Jenkins..."
systemctl restart jenkins
fi
done
π¬ Output:
[ERROR] Jenkins job queue stuckπ¨ Error detected, restarting Jenkins...
Example 8: Monitor NGINX Access Logs for High Load
#!/bin/bash
tail -n 100 /var/log/nginx/access.log | awk '{print $9}' | sort | uniq -c
π¬ Output:
500 3404 7
200 90
301 5
π‘ Tip: Use this for quick traffic analysis on your Middleware or DevOps pipeline servers.
⚠️ 4️⃣ Error Handling & Debugging
- Use
set -eto exit on first failure. - Redirect errors →
2>> error.logto capture exceptions. - Validate file existence before reading:
if [ ! -f "$filename" ]; then
echo "❌ File not found: $filename"
exit 1
fi
If your script suddenly exits, check for missing files or permission issues in
/var/log/messages or your custom error log.π Summary
- Read files line by line to automate configuration or host processing.
- Write and append logs for auditing and historical tracking.
- Monitor live logs (Tomcat, Jenkins, NGINX) using
tail -f. - Add error handling to avoid silent file issues in production.
No comments:
Post a Comment