Cron Job
You'll schedule a script so it runs at 7 a.m. every day whether you remember it or not, and learn to read the log when it doesn't.
About
A script you have to remember to run is still a chore. Cron on Linux, launchd on Mac and Task Scheduler on Windows all do the same job, which is running a command on a schedule. The trap is that cron runs with a stripped-down environment, so a script that works fine in your terminal can fail silently at 3 a.m. That's why you log everything for the first week, and why the first job here is a tiny one you can watch.
At a glance
- Per requirement
- 15 XP
- Completion bonus
- 75 XP
- Requirements
- 6
- Takes
- One week
- Skill level
- Beginner (done it a few times)
- Cost
- Free
- Supplies
- Nothing special
- Where
- At home
- Other people
- On your own
Requirements
-
1
Schedule a one-line job that appends the date to a file every minute. Open crontab -e and add a line with five stars, then the date command, then two greater-than signs and the path to a log file. Wait three minutes and open the file. If it's empty, either cron isn't running or the path is wrong, and you want to fix that before anything else. Take a screenshot of the log with at least three lines in it. Photo
-
2
Write three schedules by hand from the five fields, without a generator. The fields are minute, hour, day of month, month and day of week. Write out every weekday at 7:30, the first of each month at midnight, and every 15 minutes between 9 and 5. Check your answers against a cron explainer site afterwards rather than before, because the point is to be able to read a line cold. Take a photo of your three lines and the checked answers. Photo
-
3
Schedule your real script with full paths and its output going to a log. Cron doesn't know your aliases or your PATH, so write /usr/bin/python3 rather than python3, and use full paths for every file. Redirect both output and errors to a log file so that you see failures as well as successes. Take a screenshot of the crontab line. Photo
-
4
Break it on purpose, then find the error in the log. Rename the script and wait for the next run. The log should say it can't find the file, and that's the kind of line you'll be hunting for at 3 a.m. one day, so get to know it now. Put the name back afterwards. Take a screenshot of the error line. Photo
-
5
Let it run for seven days and show the log. Seven runs means seven entries with no gaps. If there's a gap, your laptop was probably asleep, and cron doesn't wake it. That's the reason jobs tend to move to an always-on machine later on. Take a screenshot of the week of log lines. Photo
-
6
Delete the every-minute test job. Leave it running and the log grows by about half a million lines a year. Open crontab -e, remove the line, and save. Take a screenshot of the crontab without it. Photo