Courseiva

XK0-006 Automation, Orchestration, and Scripting Practice Question

A Linux administrator is writing a Bash script that must run a cleanup routine when the script exits, whether it exits normally or is interrupted by a signal. The script defines a function named cleanup. Which command should the administrator use to ensure cleanup runs on exit?

⚠ Common exam trap

The trap here is assuming that any signal name works with trap, when SIGKILL and SIGSTOP are uncatchable and can never trigger a handler.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

trap 'cleanup' EXIT

The trap builtin is the standard Bash mechanism for running commands in response to signals or shell events. Using the EXIT pseudo-signal makes the handler run on any script termination, including normal completion and receipt of catchable signals, which is exactly what the administrator needs for a guaranteed cleanup routine.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    setsid cleanup &

    Why it's wrong here

    setsid starts the cleanup command in a new session detached from the controlling terminal, running it immediately in the background. It does not tie execution to the script's exit, does not handle signals, and would run cleanup at the wrong time, potentially before the script has finished its work.

  • ✓

    trap 'cleanup' EXIT

    Why this is correct

    The trap builtin with the EXIT pseudo-signal runs the specified command when the shell exits, regardless of whether the exit is normal or caused by a signal such as SIGINT. This matches the requirement to run cleanup unconditionally at script termination, and the single quotes defer expansion of cleanup until the trap fires.

  • ✗

    at now + 1 minute <<< cleanup

    Why it's wrong here

    The at command schedules a job to run at a future time through the atd daemon, which is unrelated to script exit handling. It would not run cleanup when the script terminates, and it depends on the at daemon being installed and running, adding an unnecessary external dependency that does not satisfy the scenario.

  • ✗

    trap 'cleanup' SIGKILL

    Why it's wrong here

    SIGKILL cannot be caught, blocked, or ignored by any process, so a trap on SIGKILL will never execute. Even though the syntax is accepted, the handler will never fire when the process is killed with SIGKILL, and a normal exit would not trigger it either, so the cleanup routine would not run as required.

About these practice questions

Courseiva writes every XK0-006 question from scratch — 781 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official CompTIA exam blueprint

This XK0-006 practice question is part of Courseiva's free CompTIA certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the XK0-006 exam.