Categories We Write About

Schedule Python scripts using cron jobs

Scheduling Python scripts using cron jobs is a common and efficient method for automating tasks on Unix-like operating systems such as Linux and macOS. Cron is a time-based job scheduler that allows users to execute scripts or commands at specified intervals. Here’s a detailed guide on how to schedule Python scripts using cron jobs, covering setup, syntax, examples, logging, and troubleshooting.

Understanding Cron and Crontab

Cron uses a configuration file called crontab to manage the scheduling of jobs. Each user on a system can have their own crontab file, which lists the jobs they want to run and when.

Each line in a crontab file follows a specific format:

lua
* * * * * command_to_run - - - - - | | | | | | | | | +----- Day of the week (0 - 7) [Sunday = 0 or 7] | | | +------- Month (1 - 12) | | +--------- Day of the month (1 - 31) | +----------- Hour (0 - 23) +------------- Minute (0 - 59)

Step-by-Step Guide to Scheduling Python Scripts

1. Prepare Your Python Script

Ensure that your Python script is executable and has the correct shebang line at the top, which tells the system where to find the Python interpreter. Example:

python
#!/usr/bin/env python3 print("Script executed")

Save the file, for example, as myscript.py, and make it executable:

bash
chmod +x myscript.py

2. Find the Full Path to Python and Your Script

Cron does not load the same environment variables as your shell. Always use the full path to both the Python interpreter and the script. You can find the path to Python using:

bash
which python3

Suppose it returns /usr/bin/python3, and your script is located at /home/user/scripts/myscript.py.

3. Open Crontab Editor

Edit the crontab file for the current user:

bash
crontab -e

This opens the user’s crontab in the default text editor.

4. Add a Cron Job Entry

To run the script every day at 6 AM, add the following line:

bash
0 6 * * * /usr/bin/python3 /home/user/scripts/myscript.py >> /home/user/logs/myscript.log 2>&1

This configuration:

  • Executes the script at 6:00 AM every day

  • Appends standard output and errors to a log file at /home/user/logs/myscript.log

5. Save and Exit

After saving and exiting the crontab editor, the cron daemon will automatically pick up the changes. You can verify the installed jobs using:

bash
crontab -l

Useful Cron Timing Examples

  • Every minute: * * * * *

  • Every 5 minutes: */5 * * * *

  • Every hour: 0 * * * *

  • Every day at midnight: 0 0 * * *

  • Every Monday at 9 AM: 0 9 * * 1

  • First day of the month at 1 AM: 0 1 1 * *

Best Practices for Cron Jobs with Python Scripts

1. Use Virtual Environments

If your Python script uses a virtual environment, make sure to activate it in the cron command:

bash
* * * * * /home/user/venv/bin/python /home/user/scripts/myscript.py

Or write a wrapper shell script to activate the virtual environment and run the script:

bash
#!/bin/bash source /home/user/venv/bin/activate python /home/user/scripts/myscript.py

Then call the shell script from cron:

bash
* * * * * /home/user/run_myscript.sh

2. Logging and Monitoring

Always log your script output. Redirect both stdout and stderr:

bash
>> /path/to/logfile.log 2>&1

Check log files regularly to catch errors and verify execution.

3. Use Absolute Paths

Avoid relative paths in your scripts when used with cron. Cron runs in a minimal shell environment, so always use full paths for files, executables, and dependencies.

4. Debugging Cron Jobs

If your script doesn’t seem to run:

  • Add a line at the top of the script to log time: print("Started at", datetime.now())

  • Use env to inspect environment variables: * * * * * env > /tmp/env.output

  • Ensure the user running the cron job has permission to execute the script

  • Ensure there are no syntax errors in the script

5. Email Notifications

By default, cron sends email to the user if there’s output. You can set the email address with the MAILTO variable:

bash
MAILTO="you@example.com" 0 6 * * * /usr/bin/python3 /home/user/scripts/myscript.py

To suppress email notifications:

bash
0 6 * * * /usr/bin/python3 /home/user/scripts/myscript.py > /dev/null 2>&1

Managing Cron Jobs

List Existing Jobs

bash
crontab -l

Remove All Jobs

bash
crontab -r

Edit Jobs

bash
crontab -e

System-Wide Cron Jobs

System-wide cron jobs are stored in:

  • /etc/crontab

  • /etc/cron.d/

  • /etc/cron.daily/, /etc/cron.hourly/, /etc/cron.weekly/, etc.

They typically include a sixth field for the user that the script should run as.

Example in /etc/crontab:

swift
0 2 * * * root /usr/bin/python3 /opt/scripts/system_cleanup.py

Alternatives to Cron

If you need more advanced scheduling options or cross-platform solutions, consider:

  • systemd timers (Linux-specific)

  • at for one-time tasks

  • Task Scheduler on Windows

  • Python libraries like APScheduler, schedule, or Celery for programmatic scheduling

Conclusion

Using cron jobs to schedule Python scripts is a powerful way to automate repetitive tasks. By correctly setting up the script, specifying absolute paths, managing logs, and adhering to best practices, you can ensure that your scheduled tasks run reliably and efficiently.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About