Challenge - 5 Problems
Signal Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2: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
Attempts:
2 left
💡 Hint
Think about what the trap command does when SIGINT is received.
✗ Incorrect
The trap command catches the SIGINT signal and runs the echo and exit commands. So the script prints 'Running...' repeatedly until Ctrl+C is pressed, then prints the message and exits.
💻 Command Output
intermediate2: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
Attempts:
2 left
💡 Hint
What is the default behavior of SIGTERM if not trapped?
✗ Incorrect
By default, SIGTERM causes the process to terminate immediately without running any custom code.
📝 Syntax
advanced2: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 hereAttempts:
2 left
💡 Hint
trap expects a command string or function name without parentheses.
✗ Incorrect
Option B correctly passes the function name without quotes or parentheses. Options B and C pass a string that calls the function but as a string literal, which works but is less direct. Option B is invalid syntax.
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
What happens after the trap command runs?
✗ Incorrect
The trap prints the message but does not exit, so the script continues running. Adding 'exit' after echo fixes this.
🚀 Application
expert2: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
Attempts:
2 left
💡 Hint
EXIT trap runs on script exit regardless of reason.
✗ Incorrect
The EXIT trap runs when the script exits for any reason, including signals that cause termination. Using EXIT alone ensures cleanup. Adding exit in the trap for signals can cause double exit calls or missed cleanup.