0
0
Bash Scriptingscripting~20 mins

Lock files for single instance in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Lock File Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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
ALock acquired. Running script.
BAnother instance is running. Exiting.
CNo output, script runs silently.
DError: Permission denied creating lock file.
Attempts:
2 left
💡 Hint
Check what happens if the lock file already exists before the script runs.
📝 Syntax
intermediate
2: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?
Aif mkdir /tmp/mylockdir 2>/dev/null; then echo "Lock acquired"; else echo "Lock exists"; fi
Bif touch /tmp/mylockfile.lock; then echo "Lock acquired"; else echo "Lock exists"; fi
Cif [ ! -e /tmp/mylockfile.lock ]; then touch /tmp/mylockfile.lock; echo "Lock acquired"; else echo "Lock exists"; fi
Dif rm /tmp/mylockfile.lock; then echo "Lock acquired"; else echo "Lock exists"; fi
Attempts:
2 left
💡 Hint
Creating a directory is atomic in Unix filesystems.
🔧 Debug
advanced
2: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
ABecause the script removes the lock file too quickly.
BBecause the script does not check if the lock file is writable.
CBecause the script does not use sudo to create the lock file.
DBecause checking -e and then touch is not atomic, two instances can create the lock file simultaneously.
Attempts:
2 left
💡 Hint
Think about what happens if two scripts check for the file at the same time.
🚀 Application
advanced
2: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?
AUse rm -f on the lock file without any checks.
BAlways remove the lock file at script end without checking.
CStore the current process ID (PID) inside the lock file and check it before removing the file.
DIgnore the lock file and create a new one with a different name.
Attempts:
2 left
💡 Hint
Think about how to avoid removing a lock held by another process.
🧠 Conceptual
expert
2: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?
Aflock provides atomic locking with automatic cleanup on process exit, avoiding stale locks.
Bflock creates lock files faster than touch or mkdir.
Cflock allows multiple scripts to share the same lock simultaneously.
Dflock automatically retries lock acquisition indefinitely without blocking.
Attempts:
2 left
💡 Hint
Consider what happens if a script crashes with manual lock files.