Linux Cron Jobs u0026 Task Scheduling Quiz Quiz

Explore your understanding of Linux cron jobs and task scheduling with this focused quiz. Enhance your knowledge of scheduling syntax, special strings, environment variables, and troubleshooting common cron issues.

  1. Cron Syntax Basics

    What is the correct syntax to schedule a cron job that runs every day at 3:00 am?

    1. 3 0 * * *
    2. * 3 0 * *
    3. 0 0 3 * *
    4. 0 3 * * *

    Explanation: The correct answer, 0 3 * * *, specifies the minute (0), hour (3), and wildcards for the rest, meaning 3:00 am every day. Option 0 0 3 * * schedules a job at midnight on the third day of each month. Option * 3 0 * * would run every minute during the third hour on Sunday only. Option 3 0 * * * attempts to run at 12:03 am daily, not 3:00 am. Understanding the order of cron fields is crucial for proper scheduling.

  2. Special Strings in Cron

    Which special string in a user's crontab runs a job at 1:00 am every day?

    1. @hourly
    2. @monthly
    3. @reboot
    4. @daily

    Explanation: @daily is a shorthand that schedules a job to run once every day, typically at 12:00 am or 1:00 am depending on implementation. @reboot schedules the job to run only once at system startup. @hourly runs the job at the beginning of every hour, while @monthly triggers it once every month. Using special strings simplifies common scheduling intervals in cron.

  3. Environment Variables in Cron

    Why might a script that runs correctly in the terminal fail when scheduled with cron?

    1. The script cannot be executed in the background.
    2. Cron jobs ignore file permissions.
    3. The script lacks access to interactive shell environment variables.
    4. Cron runs scripts as the wrong user by default.

    Explanation: Cron runs in a non-interactive shell with a limited set of environment variables, which can cause scripts relying on user-defined variables (like PATH) to fail. Cron still respects file permissions, so option two is incorrect. Cron executes jobs as the user who owns the crontab, unless specifically set otherwise, making option three wrong. Option four is incorrect because cron jobs run in the background by default unless specified otherwise.

  4. Crontab Management

    Which command displays the current user's scheduled cron jobs in a typical Linux environment?

    1. crontab -l
    2. crontab --show
    3. cron -display
    4. cronlist

    Explanation: crontab -l lists the current user's scheduled cron jobs. The command cronlist does not exist and would result in an error. crontab --show is not a valid option for the utility, and cron -display is not a recognized command. Familiarity with the correct command syntax helps in managing scheduled tasks efficiently.

  5. Error Handling in Cron Jobs

    If a cron job fails and produces output or errors, where are these by default sent in a standard user crontab setup?

    1. Sent via email to the user's local mailbox
    2. Deleted with no record kept
    3. Displayed on the user's active terminal
    4. Saved to /var/log/cron.error

    Explanation: By default, cron captures output and errors from jobs and sends them via email to the job owner's local mailbox, accessible with mail utilities. Output is not automatically saved to /var/log/cron.error unless redirected by the user. Output is not displayed on any users' terminals, since cron jobs run in the background. The output is not deleted unless specifically discarded in the cron command.