0
0
Linux CLIscripting~5 mins

crontab syntax in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Crontab syntax lets you schedule tasks to run automatically at specific times on your Linux system. It helps you automate repetitive jobs like backups or updates without needing to run them manually.
When you want to run a script every day at midnight to clean temporary files.
When you need to send a report email every Monday morning automatically.
When you want to check system health every hour without logging in.
When you want to update software packages weekly without manual intervention.
When you want to restart a service every day to keep it fresh.
Commands
This command lists all the scheduled cron jobs for the current user so you can see what tasks are set to run.
Terminal
crontab -l
Expected OutputExpected
30 2 * * * /home/user/backup.sh 0 9 * * 1 /home/user/send_report.sh
-l - List current user's cron jobs
This command schedules a new cron job to run the cleanup script every day at midnight by writing the job directly into crontab.
Terminal
echo "0 0 * * * /home/user/cleanup.sh" | crontab -
Expected OutputExpected
No output (command runs silently)
This opens the crontab file in an editor so you can add, edit, or remove scheduled tasks manually.
Terminal
crontab -e
Expected OutputExpected
No output (command runs silently)
-e - Edit the current user's crontab
This command removes all cron jobs for the current user, clearing the schedule completely.
Terminal
crontab -r
Expected OutputExpected
No output (command runs silently)
-r - Remove all cron jobs for the current user
Key Concept

If you remember nothing else from crontab syntax, remember: the five time fields (minute, hour, day of month, month, day of week) control when your task runs.

Common Mistakes
Using wrong numbers or ranges in the time fields, like 60 for minutes or 25 for hours.
Crontab expects minutes 0-59 and hours 0-23; invalid numbers cause the job to not run.
Always use valid ranges: minutes 0-59, hours 0-23, day of month 1-31, month 1-12, day of week 0-7 (0 and 7 both mean Sunday).
Forgetting to specify the full path to scripts or commands in the cron job.
Cron runs with a limited environment and may not find commands without full paths, causing failures.
Always use absolute paths like /home/user/script.sh or /usr/bin/python in your cron jobs.
Editing crontab file directly without using 'crontab -e'.
Direct edits to crontab files may not be recognized by the cron daemon, so changes won't apply.
Always use 'crontab -e' to edit cron jobs safely and apply changes immediately.
Summary
Use 'crontab -l' to list your current scheduled tasks.
Add new jobs by editing with 'crontab -e' or piping commands into crontab.
Remember the five time fields control when your task runs: minute, hour, day of month, month, day of week.