0
0
Linux-cliHow-ToBeginner · 3 min read

How to Schedule Tasks in Linux Using Cron

To schedule a task in Linux, use the cron service by adding commands to your crontab file with a specific time pattern. The crontab -e command opens the editor to create or edit scheduled tasks that run automatically at set times.
📐

Syntax

The basic syntax for scheduling a task with cron in Linux is:

  • minute hour day_of_month month day_of_week command

Each field defines when the command runs:

  • minute: 0-59
  • hour: 0-23
  • day_of_month: 1-31
  • month: 1-12
  • day_of_week: 0-7 (0 or 7 is Sunday)
  • command: the script or command to run

Use * to mean "every" for any field.

bash
*/5 * * * * /path/to/script.sh
💻

Example

This example schedules a script to run every day at 7:30 AM.

bash
30 7 * * * /home/user/backup.sh
Output
No output is shown immediately; the script runs automatically at 7:30 AM daily.
⚠️

Common Pitfalls

Common mistakes when scheduling tasks in Linux include:

  • Not using full paths for commands or scripts, causing failures.
  • Editing the wrong user's crontab or forgetting to save changes.
  • Not setting executable permissions on scripts.
  • Assuming environment variables like PATH are the same as your shell.

Always test your commands manually before scheduling.

bash
# Wrong:
* * * * * backup.sh

# Right:
* * * * * /home/user/backup.sh
📊

Quick Reference

FieldAllowed ValuesDescription
minute0-59Minute of the hour
hour0-23Hour of the day
day_of_month1-31Day of the month
month1-12Month of the year
day_of_week0-7Day of the week (0 or 7 is Sunday)
commandAny valid commandScript or command to run

Key Takeaways

Use crontab -e to edit your scheduled tasks safely.
Specify exact times using five fields before the command in crontab.
Always use full paths for scripts and commands in cron jobs.
Test commands manually before scheduling to avoid silent failures.
Remember cron runs with a limited environment; set variables if needed.