Skip to content

Scheduling Scripts on Linux/Mac (Cron Jobs)

diagram why a script that works in your shell fails under cron mermaid
Cron does not run your shell. It starts the job with a minimal environment, a different working directory and no virtualenv, so anything that relied on those is missing. Almost every 'it works when I run it manually' report comes from one of the three boxes on the right.

Cron runs commands on a schedule.

Common gotchas:

  • minimal PATH
  • different working directory
  • use absolute paths
  • redirect outputs

Example crontab entry:

text
0 9 * * * /usr/bin/python3 /home/user/job.py >> /home/user/job.log 2>&1

Use logging in the script, not only stdout.

Exercise 2 – Join Paths with os.path.join

Section titled “Exercise 2 – Join Paths with os.path.join”

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading