Trap a script saviour

Hi, How are you !! Hope you doing good....
I got introduced to Cloud initially. As I went ahead learning what is cloud and how it works, then got to know a field which is DevOps that makes Cloud model more effective.
So, as I started working & got good experience on AWS. I have been learning the DevOps tool and technologies on how to use it with the Cloud, which will give me good understanding on how Cloud and DevOps go hand in hand to deploy my applications.
Last Blog Review →
In the last blog we understood, the process priority can scheduled manually during it’s launch process via. nice cmd. But if a running process’s priority has to be re-scheduled then it can done via. renice command.
Today’s Agenda → Understanding what is Trap in Linux
What is trap ??
Let’s understand with a simple example. You as a Software engineer write a script, in which you create a temporary file to store the data and at the end of the script you delete the temporary file. Now when the script executes, suddenly the script receives a signal of exit say with crtl+c this leads to the un-expected end of the script execution that too in between. Now as the script is not executed completely the temporary file is present as it is in the file directory.
This will go un-notice, as the users wont be aware of the backend program and what it does. Which leads to the un-necessary file clot in the directory and consumption of storage space.
Solution ??
Trap comes in the picture. What it says is that, you as a software engineer tell me what all signal you want me (trap) to handle when your script runs and when i receive that/those signals, tell me what action i need to perform…
Syntax
trap action signals
Real-World DevOps Example: Graceful Shutdown of a Long-Running Script
🧩 Scenario
A DevOps engineer runs a script that:
processes logs continuously
uploads results to cloud storage
uses a lock file to prevent multiple instances
writes output to a temporary file
During deployment or server shutdown, the script gets a SIGTERM signal from systemd.
Without handling it, the script might:
leave lock files behind
leave temp files
corrupt partial data
break the next run
So DevOps engineers use trap to ensure a graceful shutdown.
🔧 Example: Log Processor With Graceful Shutdown
#!/bin/bash
LOCKFILE="/var/run/log_processor.lock"
TMPFILE="/tmp/log_buffer.txt"
LOGFILE="/var/log/log_processor.log"
# Create lock file
echo $$ > "$LOCKFILE"
# Handle shutdown signals: SIGINT (Ctrl+C), SIGTERM (systemd stop), EXIT (any exit)
trap "cleanup" SIGINT SIGTERM EXIT
cleanup() {
echo "$(date): Shutting down..." >> $LOGFILE
# Upload partial data before quitting
if [[ -s $TMPFILE ]]; then
echo "$(date): Uploading buffered data..." >> $LOGFILE
aws s3 cp $TMPFILE s3://mybucket/logs/partial_$(date +%s).txt
fi
# Remove temporary files and lock file
rm -f "$TMPFILE" "$LOCKFILE"
echo "$(date): Cleanup complete. Exiting." >> $LOGFILE
}
echo "Processing logs... (Press Ctrl+C to stop). Check the log file at /var/log/log_processor.log"
# Main worker loop
while true
do
journalctl -f | head -n 10 >> "$TMPFILE"
sleep 1
done
🧠 What This Script Does (In Simple Terms)
✔ Creates a lock file
✔ Starts processing logs
✔ Buffers data into a tmp file
✔ If interrupted by:
Ctrl+C
systemctl stopserver shutdown
script error
any exit
➡ It runs the cleanup function first.
Inside cleanup it:
uploads partial data to S3
removes lock and temp files
logs the shutdown event
exits safely
Conclusion →
In this blog we understood, what is a trap command. Why we use the trap command. The syntax of the trap command. And the real-world example of the trap command.



