CompTIA Linux+ (XK0-006)Services and User ManagementEasy
A system administrator needs to schedule a script, `/usr/local/bin/backup.sh`, to run daily at 3:00 AM. Which of the following commands would correctly add this entry to the root user's crontab?
- Acrontab -a "0 3 * * * /usr/local/bin/backup.sh"
- Becho "0 3 * * * /usr/local/bin/backup.sh" | crontab -
- Ccrontab -u root -c "0 3 * * * /usr/local/bin/backup.sh"
- Dcrontab -e "0 3 * * * /usr/local/bin/backup.sh"
Show answer & explanationAnswer & explanation
Correct answer: B. echo "0 3 * * * /usr/local/bin/backup.sh" | crontab -
To add a cron job from standard input without opening an editor, the `echo` command can pipe the cron entry directly to `crontab -`. The `-` argument tells `crontab` to read from standard input.
Why the other options are wrong
- A. The `crontab` command does not have an `-a` (add) option in this manner for direct entry.
- C. The `crontab -u` option is used to specify a user's crontab to edit, but `-c` is not a valid option for directly applying a cron entry.
- D. The `crontab -e` command opens an editor, it does not accept the cron entry as a direct argument.
Adding Cron Jobs from Standard Input
You can add cron jobs to a user's crontab directly from standard input using the `crontab -` command, often piped from `echo`.
- `crontab -` reads from stdin.
- Useful for scripting cron job additions.
- Requires correct cron syntax (minute hour day_of_month month day_of_week command).
Memory trick: Cron's 'E' for 'Edit', 'L' for 'List', '-' for 'Input'.