Mastering the Cron-icles
Conquer the Crystal Caverns Cron Jobs
navigate: 3.6 << -- >> 3.8
Review of Concepts
Brave adventurers and seekers of knowledge, I, the mighty Dragon of the Ice Caverns, stand before you today to guide you through the mysteries of cron. The crontab system file, the keeper of scheduled tasks, resides in the depths of the /etc directory, holding the power to automate essential jobs.
Brace yourselves, for the next 10 questions that await you will test your understanding of cron's arcane wisdom and hold the key to transforming your destiny.
Brace yourselves, for the next 10 questions that await you will test your understanding of cron's arcane wisdom and hold the key to transforming your destiny.
Question 1:
You, as a system administrator, want to schedule a cron job to run a script every Monday at 9:00 AM. Which command should you use?
a) 0 9 * * 1 command_to_be_executed
b) 0 9 * * * command_to_be_executed
c) 0 9 * * 2 command_to_be_executed
d) 0 9 * * 7 command_to_be_executed
Question 2:
You want to edit your crontab file using the default editor. What command should you use if your default editor is vi?
a) crontab -e vi
b) crontab -e
c) vi crontab
d) vi -e crontab
Question 3:
You want to specify a cron job that runs every 15 minutes. Which of the following is the correct command?
a) */15 * * * * command_to_be_executed
b) 0,15,30,45 * * * * command_to_be_executed
c) 0-15 * * * * command_to_be_executed
d) 0/15 * * * * command_to_be_executed
Question 4:
You have a script that needs to run at 3:00 PM every weekday, except on public holidays. Which command should you use?
a) 0 15 * * 1-5 command_to_be_executed
b) 0 15 * * 1-5,!holiday command_to_be_executed
c) 0 15 * * 1-5,~holiday command_to_be_executed
d) 0 15 * * 1-5 !holiday command_to_be_executed
Question 5:
You want to schedule a cron job that runs on the last day of every month. Which command should you use?
a) 0 0 L * * command_to_be_executed
b) 0 0 31 * * command_to_be_executed
c) 0 0 * L * command_to_be_executed
d) 0 0 * 12 * command_to_be_executed
Question 6:
You want to schedule a cron job that runs every Monday and Friday at 8:00 AM, but only during January and February. Which command should you use?
a) 0 8 * 1,2 1,5 command_to_be_executed
b) 0 8 * 1-2 1,5 command_to_be_executed
c) 0 8 * 1,2 1-5 command_to_be_executed
d) 0 8 * 1-2 1-5 command_to_be_executed
Question 7:
You want to schedule a cron job that runs every hour from 9:00 AM to 6:00 PM on weekdays. Which command should you use?
a) 0 9-18 * * 1-5 command_to_be_executed
b) 0 9,10,11,12,13,14,15,16,17,18 * * 1-5 command_to_be_executed
c) 0 9-17 * * 1-5 command_to_be_executed
d) 0 9-18 * * 0-5 command_to_be_executed
Question 8:
You want to schedule a cron job that runs every 2 hours on the 15th day of every month, starting at 8:00 AM. Which command should you use?
a) 0 8-22/2 15 * * command_to_be_executed
b) 0 8/2 15 * * command_to_be_executed
c) 0 8-22/2 15-16 * * command_to_be_executed
d) 0 8/2 15-16 * * command_to_be_executed
Question 9:
You want to schedule a cron job that runs on the second Sunday of every month. Which command should you use?
a) 0 0 * * 7#2 command_to_be_executed
b) 0 0 8-14 * 0 command_to_be_executed
c) 0 0 * * 7/2 command_to_be_executed
d) 0 0 * * 0#2 command_to_be_executed
Question 10:
You want to schedule a cron job that runs every day at 6:00 AM, except for the first day of the month. Which command should you use?
a) 0 6 2-31 * * command_to_be_executed
b) 0 6 * * * command_to_be_executed
c) 0 6 1 * * command_to_be_executed
d) 0 6 1-31/1 * * command_to_be_executed
Answers
Answer to 1:
Correct Answer:
a) 0 9 * * 1 command_to_be_executed
Explanation:
The cron expression 0 9 * * 1 specifies that the command should run at 9:00 AM (0 minutes, 9 hour) on every Monday (1 indicates Monday).
a) 0 9 * * 1 command_to_be_executed
Explanation:
The cron expression 0 9 * * 1 specifies that the command should run at 9:00 AM (0 minutes, 9 hour) on every Monday (1 indicates Monday).
Incorrect options:
Option b: This will run the command every day at 9:00 AM, not just on Mondays.
Option c: This will run the command every Tuesday, not Monday.
Option d: This will run the command every Sunday, not Monday.
Option b: This will run the command every day at 9:00 AM, not just on Mondays.
Option c: This will run the command every Tuesday, not Monday.
Option d: This will run the command every Sunday, not Monday.
Answer to 2:
Correct Answer:
b) crontab -e
Explanation:
The command crontab -e opens the crontab file in the default editor, which is defined by the EDITOR environment variable (usually vi).Incorrect options:
b) crontab -e
Explanation:
The command crontab -e opens the crontab file in the default editor, which is defined by the EDITOR environment variable (usually vi).Incorrect options:
Option a: -e is not a valid option for the vi command.
Option c: This will open the file named crontab using the vi editor, not your crontab file.
Option d: The correct order is vi -e, not -e vi.
Answer to 3:
Correct Answer:
a) */15 * * * * command_to_be_executed
a) */15 * * * * command_to_be_executed
Explanation:
The cron expression */15 * * * * specifies that the command should run every 15 minutes (*/15) of every hour (*) every day of the month (*) every month (*) and every day of the week (*).
Incorrect options:
Option b: This will run the command at the specified minutes but only in the first hour.
Option c: This will run the command every minute between 0 and 15 of every hour, not every 15 minutes.
Option d: The correct syntax for step values is */15, not 0/15.
Answer to 4:
Correct Answer:
a) 0 15 * * 1-5 command_to_be_executed
Explanation:
The cron expression 0 15 * * 1-5 specifies that the command should run at 3:00 PM (0 minutes, 15 hour) from Monday to Friday (1-5).
Public holidays are not directly supported in cron job syntax, so handling them would require additional logic within the executed script.
Incorrect options:
Option b: There is no direct support for checking holidays within cron job syntax.
Option c: The ~ symbol does not represent negation in cron job syntax.
Option d: The negation (!) should be placed before the day specification.
a) 0 15 * * 1-5 command_to_be_executed
Explanation:
The cron expression 0 15 * * 1-5 specifies that the command should run at 3:00 PM (0 minutes, 15 hour) from Monday to Friday (1-5).
Public holidays are not directly supported in cron job syntax, so handling them would require additional logic within the executed script.
Incorrect options:
Option b: There is no direct support for checking holidays within cron job syntax.
Option c: The ~ symbol does not represent negation in cron job syntax.
Option d: The negation (!) should be placed before the day specification.
Answer to 5:
Correct Answer:
a) 0 0 28-31 * * [ "$(date +\%d -d tomorrow)" == "01" ] && command_to_be_executed
Explanation:
Since cron does not directly support the concept of the "last day of the month," a workaround is needed. This command uses the date command to check if tomorrow's date is "01" (indicating a new month). If it is, the command is executed.
Incorrect options:
Option b: This will run the command only on the 31st day of every month, not considering months with fewer than 31 days.
Option c: The L (last day) should be placed in the day of the month field, not the hour field.
Option d: The asterisk (*) should be used to indicate any value for the day of the month field, not the month field.
a) 0 0 28-31 * * [ "$(date +\%d -d tomorrow)" == "01" ] && command_to_be_executed
Explanation:
Since cron does not directly support the concept of the "last day of the month," a workaround is needed. This command uses the date command to check if tomorrow's date is "01" (indicating a new month). If it is, the command is executed.
Incorrect options:
Option b: This will run the command only on the 31st day of every month, not considering months with fewer than 31 days.
Option c: The L (last day) should be placed in the day of the month field, not the hour field.
Option d: The asterisk (*) should be used to indicate any value for the day of the month field, not the month field.
Answer to 6:
Correct Answer:
d) 0 8 * 1-2 1,5 command_to_be_executed
Explanation:
The cron expression 0 8 * 1-2 1,5 specifies that the command should run at 8:00 AM (0 minutes, 8 hour) on every Monday (1) and Friday (5) during the months of January and February (1-2).
d) 0 8 * 1-2 1,5 command_to_be_executed
Explanation:
The cron expression 0 8 * 1-2 1,5 specifies that the command should run at 8:00 AM (0 minutes, 8 hour) on every Monday (1) and Friday (5) during the months of January and February (1-2).
Incorrect options:
Option a: The day of the week field should be specified as 1,5 to represent Monday and Friday.
Option b: The month field should be specified as 1-2 for January and February, not 1,2.
Option c: The day of the month field should be specified as * to represent all days of the month, not 1-5.
Option a: The day of the week field should be specified as 1,5 to represent Monday and Friday.
Option b: The month field should be specified as 1-2 for January and February, not 1,2.
Option c: The day of the month field should be specified as * to represent all days of the month, not 1-5.
Answer to 7:
Correct Answer:
a) 0 9-18 * * 1-5 command_to_be_executed
Explanation:
The cron expression 0 9-18 * * 1-5 specifies that the command should run at 0 minutes past every hour between 9:00 AM and 6:00 PM (9-18) on every weekday (1-5).
Incorrect options:
Option b: Although it lists all the hours individually, it could be simplified using a range.
Option c: This will run the script until 5:00 PM, not 6:00 PM.
Option d: The cron syntax for the day of the week field uses numbers from 0 to 7, where 0 and 7 represent Sunday.
a) 0 9-18 * * 1-5 command_to_be_executed
Explanation:
The cron expression 0 9-18 * * 1-5 specifies that the command should run at 0 minutes past every hour between 9:00 AM and 6:00 PM (9-18) on every weekday (1-5).
Incorrect options:
Option b: Although it lists all the hours individually, it could be simplified using a range.
Option c: This will run the script until 5:00 PM, not 6:00 PM.
Option d: The cron syntax for the day of the week field uses numbers from 0 to 7, where 0 and 7 represent Sunday.
Answer to 8:
Correct Answer:
c) 0 8-22/2 15 * * command_to_be_executed
Explanation:
The cron expression 0 8-22/2 15 * * specifies that the command should run at 0 minutes past every 2 hours (8-22/2) on the 15th day of every month.
c) 0 8-22/2 15 * * command_to_be_executed
Explanation:
The cron expression 0 8-22/2 15 * * specifies that the command should run at 0 minutes past every 2 hours (8-22/2) on the 15th day of every month.
Incorrect Options:
Option a: This will run the command every 2 hours starting from 8:00 AM every day, not just on the 15th day of every month.
Option b: This will run the command every 2 hours starting from 8:00 AM every day, not just on the 15th day of every month.
Option d: The /2 syntax should be used for specifying a range every 2 hours, not /2 after the hour field.
Option b: This will run the command every 2 hours starting from 8:00 AM every day, not just on the 15th day of every month.
Option d: The /2 syntax should be used for specifying a range every 2 hours, not /2 after the hour field.
Answer to 9:
Correct Answer:
a) 0 0 * * 7#2 command_to_be_executed
Explanation:
The cron expression 0 0 * * 7#2 specifies that the command should run at 0 minutes past midnight (0 minutes, 0 hour) on the second Sunday of every month (7#2).
Incorrect options:
Option b: This will run the command on every Sunday between the 8th and 14th days of the month.
Option c: The /2 syntax will run the command every 2 days of the week, not the second Sunday of every month.
Option d: 0#2 is not a valid syntax for specifying the second occurrence of a specific day.
a) 0 0 * * 7#2 command_to_be_executed
Explanation:
The cron expression 0 0 * * 7#2 specifies that the command should run at 0 minutes past midnight (0 minutes, 0 hour) on the second Sunday of every month (7#2).
Incorrect options:
Option b: This will run the command on every Sunday between the 8th and 14th days of the month.
Option c: The /2 syntax will run the command every 2 days of the week, not the second Sunday of every month.
Option d: 0#2 is not a valid syntax for specifying the second occurrence of a specific day.
Answer to 10:
Correct Answer:
a) 0 6 2-31 * * command_to_be_executed
Explanation:
The cron expression 0 6 2-31 * * specifies that the command should run at 6:00 AM (0 minutes, 6 hour) on every day from the 2nd to the 31st day of the month (2-31).
Incorrect options:
Option b: This will run the command every day at 6:00 AM, including the first day of the month.
Option c: This will run the command only on the first day of the month, not excluding it.
Option d: The /1 after the day of the month field is not necessary as it is the default behavior.
a) 0 6 2-31 * * command_to_be_executed
Explanation:
The cron expression 0 6 2-31 * * specifies that the command should run at 6:00 AM (0 minutes, 6 hour) on every day from the 2nd to the 31st day of the month (2-31).
Incorrect options:
Option b: This will run the command every day at 6:00 AM, including the first day of the month.
Option c: This will run the command only on the first day of the month, not excluding it.
Option d: The /1 after the day of the month field is not necessary as it is the default behavior.