Automating routine tasks is essential in any robust system administration or web development workflow. Cron jobs provide a powerful, time-based job scheduler in Unix-like operating systems, allowing you to run scripts or commands automatically at scheduled intervals.
A cron job is a scheduled task defined in the system’s crontab (cron table) that executes commands or scripts at specified times or intervals. Cron is widely used because it’s:
Lightweight
Highly customizable
Built into most Linux distributions
Daily or weekly database backups
Running PHP/Python scripts
Log rotation and cleaning temp files
Sending scheduled emails
Updating data caches or syncing files
To edit your user’s crontab (without root access):
Each cron job uses the following format:
Let’s say you want to run a custom Python script. First, create the file:
Sample script:
#!/usr/bin/env python3
import os
import datetime
log_dir = "/var/log/myapp"
threshold = 7 # days
now = datetime.datetime.now()
for filename in os.listdir(log_dir):
filepath = os.path.join(log_dir, filename)
if os.path.isfile(filepath):
file_age = now - datetime.datetime.fromtimestamp(os.path.getmtime(filepath))
if file_age.days > threshold:
os.remove(filepath)
Make the script executable:
Edit your crontab and add:
This:
Runs the script daily at 1:00 AM
Logs output to /var/log/cleanup.log
Temporarily disable a line by commenting it out with #.
To remove all jobs:
Custom cron jobs are a powerful tool for automation, maintenance, and reliability. Whether you’re managing a personal VPS, deploying backend scripts, or running production workloads, mastering cron allows you to control when and how tasks run — effortlessly.