Skip to content

Scheduling Scripts on Linux/Mac (Cron Jobs)

Cron basics

Cron runs commands on a schedule.

Common gotchas:

  • minimal PATH
  • different working directory

Pattern

  • use absolute paths
  • redirect outputs

Example crontab entry:

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

Tip

Use logging in the script, not only stdout.

๐Ÿงช Try It Yourself

Exercise 1 โ€“ List Files with os.listdir

Exercise 2 โ€“ Join Paths with os.path.join

Exercise 3 โ€“ Write and Read a File

If this helped you, consider buying me a coffee โ˜•

Buy me a coffee

Was this page helpful?

Let us know how we did