How to Run a Python Script Every 10 Minutes Using Cron on Linux


1 views

You have a Python script located at /home/ceasor/Desktop/script.py, and you want it to run every 10 minutes using cron. However, your current crontab entry (0 */2 * * *) runs it every 2 hours, not every 10 minutes.

The correct syntax for running a job every 10 minutes is:

*/10 * * * * username command

For your specific case, the entry should be:

*/10 * * * * ceasor python /home/ceasor/Desktop/script.py

Several factors could prevent your script from running:

  • Remove sudo from the cron job (cron runs with user permissions)
  • Ensure the script has executable permissions: chmod +x /home/ceasor/Desktop/script.py
  • Add Python shebang if running directly: #!/usr/bin/env python at the script's top

To verify if cron is executing your job:

# Check cron logs
sudo tail -f /var/log/syslog | grep CRON

# Alternative log location
sudo tail -f /var/log/cron

Here's a full example of setting up a 10-minute cron job:

# Edit user's crontab
crontab -e

# Add this line (replace with your actual path)
*/10 * * * * /usr/bin/python /home/ceasor/Desktop/script.py > /home/ceasor/cron.log 2>&1

# Verify the crontab
crontab -l

If you prefer using /etc/crontab, the format is slightly different:

*/10 * * * * ceasor /usr/bin/python /home/ceasor/Desktop/script.py

If your script requires specific Python environments:

# Using virtualenv
*/10 * * * * ceasor /path/to/venv/bin/python /home/ceasor/Desktop/script.py

# Using conda
*/10 * * * * ceasor /path/to/miniconda3/bin/python /home/ceasor/Desktop/script.py
  • Use absolute paths for everything
  • Redirect output to log files for debugging
  • Test your command directly in shell before adding to cron
  • Consider using python3 explicitly if needed

The user's current cron entry has several issues that prevent the Python script from executing:

0 */2   * * *   ceasor    sudo python  /home/ceasor/Desktop/script.py

First, this runs every 2 hours (not 10 minutes). Second, using sudo in crontab requires special configuration. Third, specifying the user (ceasor) in /etc/crontab is correct, but the path might be problematic.

For running every 10 minutes, you should use either:

*/10 * * * * ceasor /usr/bin/python3 /home/ceasor/Desktop/script.py

Or more explicitly:

0,10,20,30,40,50 * * * * ceasor /usr/bin/python3 /home/ceasor/Desktop/script.py

1. Always use absolute paths for both the Python interpreter and script

which python3  # Use this output in your crontab

2. Consider using the user's crontab instead of system crontab:

crontab -e  # As the ceasor user

3. Test with simple commands first:

*/10 * * * * /usr/bin/touch /home/ceasor/cron_test_$(date +\%s).txt

Check cron logs to see if the job is running:

grep CRON /var/log/syslog

Common issues include:

  • Environment variables not being set (specify them in crontab)
  • File permissions (ensure script is executable)
  • Python virtual environment not activated

For complex Python scripts, create a wrapper shell script:

#!/bin/bash
source /home/ceasor/venv/bin/activate
cd /home/ceasor/Desktop
/usr/bin/python3 script.py > /home/ceasor/cron.log 2>&1

Then schedule the wrapper:

*/10 * * * * ceasor /home/ceasor/Desktop/run_script.sh