Hello, I'm Arthur. Have you ever repeated the same task on your computer every day and wished it could run automatically?
Maybe you need to back up files, check a website, or clean up old logs.
Python and cron can help you automate these tasks on a Linux server without manually running a script every time.
In this tutorial, we'll create a simple Python script and schedule it to run automatically.
1. Create a Python Script
First, create a project directory:
mkdir daily-check
cd daily-check
Create a file named check.py:
from datetime import datetime
now = datetime.now()
with open("daily_log.txt", "a") as file:
file.write(f"Script ran at {now}\n")
print("Task completed")
This script records the date and time whenever it runs.
Test it manually:
python3 check.py
You should see a new file called daily_log.txt.
2. Understand What Cron Does
Cron is a Linux utility that runs commands automatically at scheduled times.
For example, you can configure it to run a script every day, every hour, or at a specific time.
This is useful for repetitive tasks such as:
- Checking website availability
- Generating daily reports
- Running data processing scripts
- Cleaning temporary files
- Creating scheduled backups
3. Schedule Your Python Script
Open your cron configuration:
crontab -e
Add the following line to run your script every day at 9 AM:
0 9 * * * /usr/bin/python3 /home/youruser/daily-check/check.py
Replace /home/youruser/ with the actual path to your project.
Save the file and exit.
Cron will now run the script at the scheduled time.
4. Check Whether the Script Worked
After the scheduled time, check your log file:
cat daily_log.txt
You should see a new entry with the date and time.
If the script does not run, check the cron configuration and system logs.
You can also redirect the script's output to a log file:
0 9 * * * /usr/bin/python3 /home/youruser/daily-check/check.py >> /home/youruser/daily-check/cron.log 2>&1
This makes it easier to troubleshoot errors.
5. Use Automation for Server Maintenance
Once you understand the basics, you can use cron for more useful server tasks.
For example, you could schedule a Python script to check whether your website is responding or generate a daily report about your application.
If you're running these tasks on a remote VPS, make sure the server has enough resources and that your scripts handle errors properly.
For developers comparing hosting options for Python applications, Seimaxim's VPS hosting services are worth exploring when choosing a server for scheduled tasks and other workloads.
6. Be Careful With Automated Tasks
Automation saves time, but a script that runs repeatedly can also create problems if it is not configured properly.
Before scheduling a task, make sure:
- The script works when run manually.
- File paths are correct.
- Required permissions are available.
- Errors are logged.
- The task does not run more frequently than necessary.
For important tasks, test the script in a safe environment before scheduling it on a production server.
Final Thoughts
Python and cron are a useful combination for automating repetitive work on Linux.
You can start with a simple script and gradually build more advanced tools for monitoring, reporting, and maintenance.
Learning how to schedule tasks is a practical skill for anyone working with Python or managing a Linux server.
If you need a remote environment for your Python projects, you can also explore Seimaxim for VPS hosting options.
Top comments (0)