This article provides a comprehensive introduction to the
cron
command in Linux for beginners. It covers the history ofcron
, its purpose, and when and why to use it. The guide offers a detailed explanation of how to usecron
, its commonly used parameters, and other supported parameters. Furthermore, it discusses the most common use cases and some tricky skills associated with the command. Lastly, the article highlights important points to be aware of and concludes with an overall summary.
Instructions
This article is a comprehensive guide for beginners to learn about the Linux cron
command, including its history, use cases, and various techniques for using it effectively.
History
cron
is a Unix-based job scheduler that allows users to execute commands or scripts at specified intervals. The name "cron" comes from the Greek word "chronos," which means "time." The cron
command was first introduced in Unix Version 7 in 1979, and it has been widely used ever since.
When and why to use it
cron
is best used for automating repetitive tasks, such as running backups, sending emails, or updating databases. It allows users to schedule tasks to run at specific times or intervals without manual intervention, thus increasing productivity and reducing the possibility of human error.
How to use it
To use cron
, you need to create a crontab
file containing the commands you want to schedule. You can edit your crontab
file by running the following command: +++ Bash crontab -e +++
The commonly used parameters
* * * *
: This represents the cron job schedule format, which consists of five fields representing (from left to right) minutes, hours, days of the month, months, and days of the week.
30 2 * * * /path/to/script.sh
@reboot
: This parameter allows you to run a command or script at system startup.
@reboot /path/to/script.sh
@daily
,@weekly
,@monthly
: These parameters allow you to schedule tasks to run daily, weekly, or monthly.
@daily /path/to/script.sh
>/dev/null 2>&1
: This parameter suppresses the output of the cron job, preventing it from sending emails or generating log files.
30 2 * * * /path/to/script.sh >/dev/null 2>&1
Other supported parameters
There are no other parameters for the cron
command itself, but there are other special strings like @yearly
and @hourly
that can be used in the crontab
file for convenience.
Most common use cases
- Running a backup script every night at 2:30 AM:
30 2 * * * /path/to/backup_script.sh
- Sending a weekly report email every Monday at 10 AM:
0 10 * * 1 /path/to/send_report.sh
- Updating a database every hour:
0 * * * * /path/to/update_database.sh
The tricky skills
- Running a command every 5 minutes:
*/5 * * * * /path/to/script.sh
- Running a command on the last day of every month:
59 23 28-31 * * [ "$(date +'\\%m')" != "$(date +'\\%m' -d tomorrow)" ] && /path/to/task.sh
- Running a command on the first day of every month:
0 0 1 * * /path/to/script.sh
What needs to be noted
- Ensure that the script or command being scheduled has the necessary permissions to execute.
- Make sure that the paths to the script or command are correct, as
cron
runs in a limited environment and may not have access to your regular environment variables.
Conclusion
The cron
command is a powerful tool for automating tasks in Linux. By understanding its history, use cases, and various techniques, you can harness the full potential of cron
to increase productivity and reduce manual intervention in your daily tasks. Keep practicing and experimenting with different configurations to become proficient in using the cron
command and optimizing your workflow.