0
0
Bash Scriptingscripting~10 mins

Lock files for single instance in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Lock files for single instance
Start Script
Check if lock file exists
Exit: Another instance
Run main script
Remove lock file
End Script
The script first checks if a lock file exists. If yes, it exits to avoid multiple instances. If no, it creates the lock file, runs the main task, then removes the lock file.
Execution Sample
Bash Scripting
#!/bin/bash
LOCKFILE=/tmp/myscript.lock
if [ -e "$LOCKFILE" ]; then
  echo "Already running"
  exit 1
fi
trap "rm -f $LOCKFILE" EXIT
touch "$LOCKFILE"
# main work here
sleep 2
echo "Script done"
This script uses a lock file to ensure only one instance runs. It exits if the lock file exists, otherwise creates it, runs work, then cleans up.
Execution Table
StepCheck Lock FileLock File Exists?ActionOutput
1Check if /tmp/myscript.lock existsNoCreate lock file
2Run main work (sleep 2)N/ASleep 2 seconds
3Print messageN/Aecho 'Script done'Script done
4Remove lock file on exitN/Arm -f /tmp/myscript.lock
5Script endsN/AExit normally
💡 Script ends after removing lock file to allow future runs
Variable Tracker
VariableStartAfter Step 1After Step 4Final
LOCKFILE/tmp/myscript.lock/tmp/myscript.lock (created)None (removed)None (removed)
Key Moments - 3 Insights
Why does the script exit immediately if the lock file exists?
Because the lock file signals another instance is running. The script checks this at Step 1 in the execution_table and exits to avoid conflicts.
What ensures the lock file is removed even if the script is interrupted?
The trap command sets a cleanup action on EXIT (Step 4). This means the lock file is removed no matter how the script ends.
Why do we create the lock file after checking it doesn't exist?
Creating the lock file after confirming it doesn't exist prevents race conditions. This is shown in Step 1 where the file is created only if absent.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at Step 1 if the lock file exists?
AThe script creates a new lock file
BThe script exits immediately
CThe script runs main work
DThe script removes the lock file
💡 Hint
Refer to Step 1 in execution_table where the lock file existence check leads to exit if true
According to variable_tracker, what is the state of LOCKFILE after Step 4?
ALOCKFILE is renamed
BLOCKFILE is created and exists
CLOCKFILE is removed and does not exist
DLOCKFILE is empty but exists
💡 Hint
Check variable_tracker row for LOCKFILE after Step 4 showing removal
If the trap command was missing, what would likely happen?
ALock file might remain after script ends
BLock file would never be created
CScript would run twice simultaneously
DScript would not check for lock file
💡 Hint
trap ensures cleanup at exit; without it, lock file removal at Step 4 might not happen
Concept Snapshot
Lock files prevent multiple script instances.
Check if lock file exists; if yes, exit.
If no, create lock file and run work.
Use trap to remove lock file on exit.
This avoids conflicts and ensures single instance.
Full Transcript
This bash script uses a lock file to allow only one instance to run at a time. It starts by checking if the lock file exists. If it does, the script prints a message and exits immediately to avoid running twice. If the lock file does not exist, the script creates it and sets a trap to remove it when the script ends. Then it runs the main work, here simulated by sleep 2 seconds. After finishing, the trap removes the lock file so future runs can proceed. This method prevents multiple instances from running simultaneously and causing conflicts.