Linux Basic — Scheduling with Cron Practice
Practice Exercises — Scheduling with Cron
Work through these to gain fluency with crontab editing, expressions, and logging.
Warm-up
- Write a crontab entry to run
/opt/hello.shevery minute. - Write an entry for daily at 3:30 AM.
- Write an entry for every Monday at 9 AM.
- Use
@dailyshortcut for/opt/backup.sh.
Expressions
- Every 15 minutes.
- Every 2 hours, but only between 9 AM and 5 PM.
- Weekdays only (Mon–Fri) at 8 AM.
- First and fifteenth of every month at midnight.
- Every Sunday at noon.
Environment & Debugging
- Add a cron job that runs
env > /tmp/cronenv.txtto see the cron environment. - Set
PATH=/usr/local/bin:/usr/bin:/binin your crontab. - Set
MAILTO=you@example.comand confirm cron emails you on job output. - Disable email with
MAILTO="".
Logging
- Redirect a job’s output to
/var/log/myjob.log(append mode, stderr too). - Use
logger -t myjobto send output to syslog. - Add a job that logs “hello at $(date)” every minute for 5 minutes, then remove it.
Real Scenarios
- Schedule
/opt/backup.shto run daily at 2 AM, logging to/var/log/backup.log. - Schedule
/opt/cleanup.shto run weekly on Sundays at 3 AM. - Use
@rebootto run/opt/startup.shonce at boot. - Combine: run a job every 10 minutes during work hours (9–5, Mon–Fri), log to a file, and email on failure.
Verification Checklist
- All jobs use absolute paths for scripts and commands
PATHis set explicitly in crontab if needed- Logs use
>>to append, not>to overwrite - Test each job manually before scheduling
- Verify jobs run by checking logs or temporary outputs
Next chapter → Clean & Readable Code