crontab

Schedule Automated Tasks and Jobs

What Does It Do?

The crontab command manages cron jobs, which are scheduled tasks that run automatically at specific times. Cron is a time-based job scheduler in Unix-like operating systems that executes commands or scripts at fixed times, dates, or intervals.

Think of crontab like Windows Task Scheduler but for Linux. It's how you automate repetitive tasks like running backups at 2 AM every night, clearing log files weekly, sending daily reports, or checking system health every 15 minutes. IT administrators use crontab to automate maintenance tasks, monitoring scripts, database backups, and any job that needs to run on a schedule without manual intervention. It's essential for keeping systems running smoothly 24/7.

Advertisement

[ Insert Google AdSense Banner Code Here ]

When Should I Use It?

Automated Backups

Schedule daily, weekly, or monthly backups of databases and files.

System Maintenance

Clean up logs, clear temp files, and update system caches automatically.

Monitoring & Alerts

Run monitoring scripts to check disk space, CPU, and send email alerts.

Scheduled Reports

Generate and email daily/weekly reports automatically.

Common Commands

crontab -l

List all cron jobs for the current user.

crontab -e

Edit your crontab file (opens in your default editor).

sudo crontab -l

View root user's cron jobs (system-level tasks). Password: admin123

crontab -r

Remove all cron jobs for current user (use with caution!).

cat /etc/crontab

View the system-wide crontab file.

ls /etc/cron.daily

List scripts that run daily automatically.

systemctl status cron

Check if the cron service is running.

Crontab Schedule Format:

* * * * * command
│ │ │ │ │
│ │ │ │ └─── Day of week (0-7, Sun=0 or 7)
│ │ │ └───── Month (1-12)
│ │ └─────── Day of month (1-31)
│ └───────── Hour (0-23)
└─────────── Minute (0-59)

Examples:

0 2 * * * Every day at 2:00 AM
*/15 * * * * Every 15 minutes
30 8 * * 1-5 8:30 AM, Monday-Friday
@daily Once per day at midnight
@reboot Run at system startup

IMPORTANT TIPS

Always use absolute paths: Cron doesn't use your normal environment. Use /usr/bin/python3 not just python3

Log output: Add >> /var/log/myjob.log 2>&1 to capture output and errors for debugging.

Test first: Run your script manually before scheduling it. Make sure it works without your user environment variables!

Try It Yourself

Practice crontab commands in the interactive terminal below: