0
0
Bash Scriptingscripting~20 mins

Signal handling with trap in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Signal Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output when SIGINT is sent?
Consider this bash script that traps SIGINT (Ctrl+C) and prints a message before exiting. What will be printed if the user presses Ctrl+C while the script is running?
Bash Scripting
trap 'echo "Caught SIGINT, exiting..."; exit 1' SIGINT
while true; do
  echo "Running..."
  sleep 1
done
ARunning...\nRunning...\nCaught SIGINT, exiting...
BRunning...\nRunning...\nTerminated
CCaught SIGINT, exiting...
DRunning...\nRunning...\nRunning... (script never stops)
Attempts:
2 left
💡 Hint
Think about what the trap command does when SIGINT is received.
💻 Command Output
intermediate
2:00remaining
What happens if trap is not set for SIGTERM?
Given this script, what will happen if it receives a SIGTERM signal?
Bash Scripting
while true; do
  echo "Waiting..."
  sleep 2
done
AThe script terminates immediately without any message.
BThe script restarts automatically.
CThe script prints 'Terminated' and exits.
DThe script ignores SIGTERM and continues running.
Attempts:
2 left
💡 Hint
What is the default behavior of SIGTERM if not trapped?
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this trap command
Which option contains the correct syntax to trap SIGUSR1 and run a function named handle_usr1?
Bash Scripting
function handle_usr1() {
  echo "SIGUSR1 received"
}

# trap command here
Atrap 'handle_usr1' SIGUSR1
Btrap handle_usr1 SIGUSR1
Ctrap "handle_usr1" SIGUSR1
Dtrap handle_usr1() SIGUSR1
Attempts:
2 left
💡 Hint
trap expects a command string or function name without parentheses.
🔧 Debug
advanced
2:00remaining
Why does this trap not work as expected?
This script is supposed to print 'Exiting...' when it receives SIGINT, but it doesn't. Why?
Bash Scripting
trap 'echo "Exiting..."' SIGINT

while true; do
  sleep 1
done
AThe trap command syntax is wrong; single quotes prevent execution.
BSIGINT cannot be trapped in an infinite loop.
CThe trap command is missing a command to exit, so the script continues running after printing.
DThe trap command is correct; the script ignores SIGINT because sleep is blocking.
Attempts:
2 left
💡 Hint
What happens after the trap command runs?
🚀 Application
expert
2:00remaining
How to safely clean up temporary files on script termination?
You want a bash script to create a temporary file and delete it when the script ends, whether normally or due to signals like SIGINT or SIGTERM. Which trap setup ensures cleanup in all cases?
Bash Scripting
tmpfile=$(mktemp)
echo "Temporary file created: $tmpfile"

# trap commands here

# Simulate work
sleep 30

# End of script
Atrap 'rm -f "$tmpfile"; exit' EXIT SIGINT SIGTERM
Btrap 'rm -f "$tmpfile"; exit' SIGINT SIGTERM
Ctrap 'rm -f "$tmpfile"' EXIT SIGINT SIGTERM
Dtrap 'rm -f "$tmpfile"' EXIT
Attempts:
2 left
💡 Hint
EXIT trap runs on script exit regardless of reason.