Challenge - 5 Problems
Lock File Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this lock file check script?
Consider this bash script snippet that uses a lock file to ensure a single instance runs. What will it output if the lock file already exists?
Bash Scripting
#!/bin/bash LOCKFILE=/tmp/mylockfile.lock if [ -e "$LOCKFILE" ]; then echo "Another instance is running. Exiting." exit 1 else touch "$LOCKFILE" echo "Lock acquired. Running script." # Simulate work sleep 1 rm "$LOCKFILE" fi
Attempts:
2 left
💡 Hint
Check what happens if the lock file already exists before the script runs.
✗ Incorrect
The script checks if the lock file exists. If it does, it prints the message and exits. So if the lock file is already there, it outputs the message about another instance running.
📝 Syntax
intermediate2:00remaining
Which option correctly creates a lock file atomically?
You want to create a lock file only if it does not exist, atomically, to avoid race conditions. Which bash command snippet does this correctly?
Attempts:
2 left
💡 Hint
Creating a directory is atomic in Unix filesystems.
✗ Incorrect
Using mkdir to create a directory as a lock is atomic. If mkdir succeeds, the lock is acquired. Other methods like touch or checking existence are not atomic and can cause race conditions.
🔧 Debug
advanced2:00remaining
Why does this lock file script fail to prevent multiple instances?
This script is intended to prevent multiple instances by creating a lock file. Why might it fail?
#!/bin/bash
LOCKFILE=/tmp/mylockfile.lock
if [ ! -e "$LOCKFILE" ]; then
touch "$LOCKFILE"
echo "Lock acquired"
sleep 10
rm "$LOCKFILE"
else
echo "Another instance running"
fi
Attempts:
2 left
💡 Hint
Think about what happens if two scripts check for the file at the same time.
✗ Incorrect
The check for existence and creation of the lock file are two separate steps, so two scripts can both see the file missing and both create it, causing multiple instances.
🚀 Application
advanced2:00remaining
How to safely release a lock file only if owned by current process?
You want to release a lock file only if it was created by the current script instance. Which approach is safest?
Attempts:
2 left
💡 Hint
Think about how to avoid removing a lock held by another process.
✗ Incorrect
Writing the PID inside the lock file and verifying it before removal ensures only the owner process removes the lock, preventing accidental unlocking by others.
🧠 Conceptual
expert2:00remaining
What is the main advantage of using flock over manual lock files?
In bash scripting, what is the main advantage of using the flock command to manage single instance locks instead of manually creating lock files?
Attempts:
2 left
💡 Hint
Consider what happens if a script crashes with manual lock files.
✗ Incorrect
flock uses kernel-level locks that are automatically released if the process exits, preventing stale locks and race conditions common with manual lock files.