I want my Linux server to do things for me without me having to remember. For example, I might want a Python script to back up some files every night. Or maybe I want a program to check the weather and send me an email every morning. That’s where Cron comes in. It’s like an alarm clock for your computer. You set it once, and it runs your code for you—day or night, rain or shine.
In this post, I’ll show you how I use Cron to run Python scripts automatically. It’s not as scary as it sounds. I’ll explain every step, share a few tips, and even throw in some bad computer puns. So buckle up—let’s make your computer a little smarter and a lot more helpful.
What Is Cron?
Cron (pronounced like “brawn” without the “b”) is a tool that comes with most Linux and Unix systems. It’s used to schedule tasks, which are called cron jobs. These jobs run on a schedule you set. Think of it like setting a timer to bake cookies—except it’s for code.
Here’s a real-life example: I once wrote a Python script that deleted old files I didn’t need anymore. Instead of running it by hand every week, I told Cron to do it for me every Sunday night. It worked like magic—except with fewer sparks.
Why Use Cron?
Let’s look at why Cron is useful:
- You can run scripts without clicking anything.
- It works on autopilot—even while you’re asleep.
- It keeps things on track and saves time.
Have you ever forgotten to run a script or check something important? I sure have. Cron helps avoid that.
What You Need Before You Start
Before setting up a cron job, make sure you have:
- A Python script you want to run
- A computer that runs Linux, macOS, or Windows with WSL (Windows Subsystem for Linux)
- Some basic command line knowledge
Also, make sure Python is installed and your script runs correctly when you type:
python3 your_script.py
If that works, you’re halfway there.
A Quick Note About Paths
One thing that confused me at first was file paths. Cron doesn’t always know where your files are. So you have to give it the full absolute path, not just “script.py.”
For example, instead of this:
python3 script.py
Use this:
python3 /home/yourname/projects/script.py
Cron needs full directions, like your friend who always gets lost without GPS.
How to Edit the Cron Table
To set up a cron job, you edit something called a crontab. That’s short for “cron table.” It’s where you tell Cron what to do and when to do it.
Open a terminal and type:
crontab -e
This opens the crontab file in your default text editor. If it asks you to choose an editor, I recommend using nano
if you’re new. It’s simple and easy to use.
At the bottom of the file, you can add your job.
The Cron Time Format
Here’s where things get a little tricky (but I’ll help you).
Cron uses five numbers to set the time:
* * * * * command_to_run
Each star means:
- Minute (0–59)
- Hour (0–23)
- Day of the month (1–31)
- Month (1–12)
- Day of the week (0–7) — Sunday is 0 or 7
Then comes the command to run.
Some Examples
0 7 * * *
→ Every day at 7:00 AM30 14 * * 1-5
→ Monday to Friday at 2:30 PM0 0 * * 0
→ Every Sunday at midnight
Let’s Add a Python Script to Cron
Let’s say you have a script at /home/yourname/scripts/hello.py
. You want it to run every day at 8 AM.
Here’s what to do:
- Open your crontab:
crontab -e
- Add this line at the bottom:
0 8 * * * /usr/bin/python3 /home/yourname/scripts/hello.py >> /home/yourname/logs/hello.log 2>&1
Let’s break that down:
0 8 * * *
→ 8:00 AM every day/usr/bin/python3
→ Full path to Python (you can find it usingwhich python3
)/home/yourname/scripts/hello.py
→ Your script>> /home/yourname/logs/hello.log
→ Save the output to a log file2>&1
→ Also save errors to that same file
Now, Cron will run that script every morning. Even if you’re still asleep.
Check That It’s Working
Want to make sure your cron job works?
Here’s what I do:
- Make the script print the date and time.
- Look at the log file after it runs.
You can also check the cron log:
grep CRON /var/log/syslog
That’s like asking Cron, “Hey buddy, did you run my job?”
Common Problems and Fixes
Cron can be picky. Here are some issues I’ve run into:
- The script doesn’t run? Make sure the path to Python and your script are correct.
- Nothing happens? Try adding print statements and checking the log file.
- It runs but fails? Maybe your script depends on environment variables. Cron doesn’t use your normal shell, so set everything inside the script.
Here’s what helps me troubleshoot:
- Always use full paths.
- Redirect output to a log file.
- Use
which python3
to get the correct path.
Quick Tips for Success
Here are some things I’ve learned the hard way:
- Give your scripts permission to run:
chmod +x script.py
- Use absolute paths, always.
- Test your job before setting a weird schedule.
- Keep logs so you can see what’s going on.
A Handy Checklist
Before adding your script to Cron, double-check this:
- ✅ The script runs manually without errors
- ✅ You used full paths to both Python and your script
- ✅ You tested it with output logging
- ✅ You used
crontab -e
(not editing the wrong file) - ✅ You checked the time format carefully
Trust me—this checklist saves headaches.
Let’s Compare: Cron vs Running Manually
Task | Manual Run | Cron Run |
---|---|---|
Need to remember | Yes | Nope |
Can run while you’re away | No | Yes |
Easy to change schedule | Kind of | Very easy |
Errors logged automatically | Not usually | Yes (if set up) |
Running things manually is fine for quick tests. But if you want reliability, Cron is your buddy.
Wrap Up
Now you know how to run Python scripts using Cron. It takes a bit of setup, but it saves you tons of time later. I use Cron for backups, reminders, file cleanups, and even sending emails. It’s like having a robot assistant who never forgets anything—though sadly, it doesn’t make coffee.
Try it out with a small script first. Maybe just have it print the date. Once that works, you’ll see how handy it is.
And hey, if you ever feel like your computer’s working too hard, just remember: it literally runs on commands.