Linux and Command-Line Basics
The line "30 2 * * 1 /home/alice/backup.sh" was registered in cron. Which of the following is when backup.sh is run?
- AAt 2:30 on the 1st of every month
- BAt 2:30 every Monday
- CAt 2:30 every Sunday
Show answer and explanation
Answer: B. At 2:30 every Monday
In a cron configuration (crontab), each line starts with five fields that are, from the left, "minute, hour, day of month, month, day of week", followed by the command to run. In this line the minute is 30, the hour 2, the day of month and month * (every), and the day of week 1, so it runs at 2:30 every Monday. For the day of week, 0 is Sunday and 1 is Monday, and in many implementations 7 also means Sunday. Commands run from cron work with a limited set of environment variables, unlike a login session, so care is needed, such as writing commands in scripts with absolute paths.
- A. For the 1st of every month, set the third field, the day of month, to 1, as in "30 2 1 * *". The fifth field is the day of week.
- B. Correct. It means minute 30, hour 2 and day of week 1 (Monday).
- C. In the day-of-week field, 0 (or 7) is Sunday and 1 is Monday.