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.
What is the correct syntax to schedule a cron job that runs every day at 3:00 am?
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.
Which special string in a user's crontab runs a job at 1:00 am every day?
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.
Why might a script that runs correctly in the terminal fail when scheduled with cron?
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.
Which command displays the current user's scheduled cron jobs in a typical Linux environment?
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.
If a cron job fails and produces output or errors, where are these by default sent in a standard user crontab setup?
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.