Bird
Raised Fist0
Bash Scriptingscripting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using a lock file in a bash script?
easy
A. To prevent multiple instances of the script from running at the same time
B. To speed up the script execution
C. To store user input temporarily
D. To log errors during script execution

Solution

  1. Step 1: Understand the role of a lock file

    A lock file is used to signal that a script is already running, preventing duplicates.
  2. Step 2: Identify the main purpose

    This prevents multiple script instances from running simultaneously, avoiding conflicts.
  3. Final Answer:

    To prevent multiple instances of the script from running at the same time -> Option A
  4. Quick Check:

    Lock file purpose = prevent duplicates [OK]
Hint: Lock files stop duplicate script runs quickly [OK]
Common Mistakes:
  • Thinking lock files speed up scripts
  • Confusing lock files with log files
  • Using lock files to store data
2. Which of the following bash commands correctly creates a lock file only if it does not already exist?
easy
A. touch /tmp/myscript.lock
B. echo $$ > /tmp/myscript.lock
C. mkdir /tmp/myscript.lock
D. if [ ! -e /tmp/myscript.lock ]; then touch /tmp/myscript.lock; fi

Solution

  1. Step 1: Check for existence before creating

    The command must create the lock file only if it does not exist, so a test with [ ! -e ] is needed.
  2. Step 2: Identify the correct syntax

    if [ ! -e /tmp/myscript.lock ]; then touch /tmp/myscript.lock; fi uses an if statement to check and then create the file, which is correct.
  3. Final Answer:

    if [ ! -e /tmp/myscript.lock ]; then touch /tmp/myscript.lock; fi -> Option D
  4. Quick Check:

    Check existence before touch = if [ ! -e /tmp/myscript.lock ]; then touch /tmp/myscript.lock; fi [OK]
Hint: Use if with [ ! -e ] to create lock file safely [OK]
Common Mistakes:
  • Using touch without checking file existence
  • Using mkdir which creates a directory, not a file
  • Overwriting lock file without check
3. Given this script snippet:
if [ -e /tmp/myscript.lock ]; then
  echo "Script already running"
  exit 1
else
  touch /tmp/myscript.lock
  echo "Running script"
  rm /tmp/myscript.lock
fi
What will be the output if the lock file exists?
medium
A. Running script
B. Script already running
C. No output
D. Error: lock file missing

Solution

  1. Step 1: Check the condition when lock file exists

    If /tmp/myscript.lock exists, the if condition is true and the script echoes "Script already running" and exits.
  2. Step 2: Understand the output

    Since the script exits early, it does not run or remove the lock file.
  3. Final Answer:

    Script already running -> Option B
  4. Quick Check:

    Lock file exists = "Script already running" [OK]
Hint: If lock file exists, script exits with message [OK]
Common Mistakes:
  • Assuming script runs even if lock file exists
  • Expecting no output when lock file exists
  • Confusing lock file presence with absence
4. This script is intended to prevent multiple runs using a lock file:
if [ -e /tmp/myscript.lock ]; then
  echo "Already running"
  exit 1
fi

touch /tmp/myscript.lock
# script work here
rm /tmp/myscript.lock
But sometimes multiple instances run simultaneously. What is the likely problem?
medium
A. The lock file is not checked before creation
B. The script does not remove the lock file
C. The lock file is created after the check, causing a race condition
D. The script uses rm instead of rmdir

Solution

  1. Step 1: Analyze the timing of lock file creation

    The script checks for the lock file, then creates it. Between these steps, another instance can start and pass the check.
  2. Step 2: Identify the race condition

    This gap causes multiple instances to run simultaneously because the lock file is not created atomically with the check.
  3. Final Answer:

    The lock file is created after the check, causing a race condition -> Option C
  4. Quick Check:

    Race condition from delayed lock creation = The lock file is created after the check, causing a race condition [OK]
Hint: Create lock file atomically to avoid race conditions [OK]
Common Mistakes:
  • Ignoring race conditions in lock file creation
  • Forgetting to remove lock file after script ends
  • Confusing file removal commands
5. You want to write a bash script that runs only one instance at a time using a lock file. Which approach is best to avoid race conditions?
hard
A. Use 'mkdir /tmp/myscript.lock' and check if it succeeds before running
B. Check if lock file exists, then create it with 'touch' if not
C. Create lock file with 'echo $$ > /tmp/myscript.lock' without checking
D. Remove lock file at script start, then create it

Solution

  1. Step 1: Understand atomic operations for locking

    Using 'mkdir' to create a directory as a lock is atomic; it either succeeds or fails immediately.
  2. Step 2: Compare with other methods

    Other methods like checking then touching can cause race conditions; removing lock at start is unsafe.
  3. Final Answer:

    Use 'mkdir /tmp/myscript.lock' and check if it succeeds before running -> Option A
  4. Quick Check:

    Atomic mkdir lock = safest method [OK]
Hint: Use mkdir for atomic lock creation to avoid race [OK]
Common Mistakes:
  • Using touch without atomic check
  • Removing lock file before checking
  • Ignoring race conditions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard