DEV Community

Kervie Sazon
Kervie Sazon

Posted on

Linux Fundamentals - Part 10: Bash Scripting (Crontab)

Crontab

Crontab stands for cron table. It’s a text file used to schedule commands or scripts to run automatically at specified times on Unix-like systems. These scheduled tasks are called cron jobs.

How Cron Jobs Work

The cron daemon checks tables of scheduled tasks and executes them accordingly. Each user can have their own crontab file. This makes cron jobs very flexible for both system-wide and user-specific automation.

Crontab Syntax

crontab -e
Enter fullscreen mode Exit fullscreen mode

This opens your crontab file in your default editor.

Each line in the file represents a scheduled task using this format:

* * * * * /full/path/to/command
| | | | |
| | | | └─ Day of Week (0–7) (Sunday = 0 or 7)
| | | └─── Month (1–12)
| | └───── Day of Month (1–31)
| └─────── Hour (0–23)
└───────── Minute (0–59)
Enter fullscreen mode Exit fullscreen mode

Crontab Examples

Run a Script Every Day at 6 AM

0 6 * * * /home/user/backup.sh
Enter fullscreen mode Exit fullscreen mode

Run Every 10 Minutes

*/10 * * * * /usr/bin/python3 /home/user/script.py
Enter fullscreen mode Exit fullscreen mode

Run Every Monday at Midnight

0 0 * * 1 /home/user/monday_job.sh
Enter fullscreen mode Exit fullscreen mode

Run at 3:30 PM on the First Day of the Month

30 15 1 * * /home/user/monthly_report.sh
Enter fullscreen mode Exit fullscreen mode

Cron Shortcuts

Cron provides special keywords to make schedules easier:

@reboot - Run at system startup
@hourly - Run every hour
@daily - Run once per day
@weekly - Run once per week
@monthly - Run once per month
@yearly - Run once per year

Example:

@daily /home/user/daily_cleanup.sh
Enter fullscreen mode Exit fullscreen mode

Viewing Your Cron Jobs

To list all cron jobs for your user:

crontab -l
Enter fullscreen mode Exit fullscreen mode

To edit again (with Vim):

crontab -e
Enter fullscreen mode Exit fullscreen mode

Tips for Cron Jobs

Always use full paths.

Example:

/usr/bin/python3 /home/user/script.py
Enter fullscreen mode Exit fullscreen mode

Make scripts executable.

chmod +x /home/user/script.sh
Enter fullscreen mode Exit fullscreen mode

Today, I learned how to use crontab to schedule tasks in Linux. I discovered how cron jobs work, how to define time schedules using cron syntax, and how to manage scheduled tasks using Vim as my editor.

Top comments (0)