Blog 15 Leveling Up Shell: Idempotent Scripts, Deployments & Log Cleanup
π₯️Idempotency, Deployment & Log Cleanup in Shell Scripting
Welcome back to the journey π In blog 14 , we learned about functions and loops. Now in blog 15, we step into real-world server automation concepts.
π 1️⃣ What is Idempotency?
Idempotent = Same result no matter how many times you run it
id ec2-user &>/dev/null || useradd ec2-user
π 2️⃣ Idempotency in HTTP Methods
| Method | Idempotent? | Why |
|---|---|---|
| GET | ✅ | Only reads data |
| PUT | ✅ | Updates same resource |
| DELETE | ✅ (mostly) | Deleting again gives “not found” |
| POST | ❌ | Creates new data each time |
π 3️⃣ Deployment Steps (Real Server)
rm -rf /app/* git clone repo-url /app cd /app && npm install systemctl restart myapp
π 4️⃣ Log Management in Linux
find /var/log/myapp -name "*.log" -mtime +14 -delete
π¦ 5️⃣ Archiving Old Logs
mkdir -p /archive/logs
find /var/log/myapp -name "*.log" -mtime +14 -exec mv {} /archive/logs \;
⏳ 6️⃣ Creating Old Files for Testing
touch -d "20 days ago" test.log
π 7️⃣ While Loop – Read File Line by Line
FILE=temp.txt while read line do echo "Processing $line" done < $FILE
π§© 8️⃣ Why Shell Scripts Are Not Naturally Idempotent
Running scripts repeatedly can reinstall packages, duplicate users, or overwrite files. We must add logic to make scripts idempotent.
π¦ 9️⃣ NPM Warnings (Informational)
Sometimes npm install shows warnings about optional dependencies.
These are not errors and don’t affect core functionality.
π§ π Final Takeaway
we moves you from writing scripts to building reliable server automation.
- Idempotency mindset
- Safe deployments
- Log rotation
- File handling with loops
© Kalyan – DevSecOps Journey
Comments
Post a Comment