Blog 15 Leveling Up Shell: Idempotent Scripts, Deployments & Log Cleanup

Idempotency, Deployment & Log Cleanup in Shell Scripting

πŸ–₯️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

MethodIdempotent?Why
GETOnly reads data
PUTUpdates same resource
DELETE✅ (mostly)Deleting again gives “not found”
POSTCreates 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

Popular Posts