The crontab command is commonly used in Unix-and Unix-like operating systems to set instructions that are executed periodically. The command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution. The word derives from the Greek language Chronos (χρνο), which was originally meant to be time. Often, crontab stored instructions are activated by the daemon, Crond often run in the background, and every minute checks whether a scheduled job needs to be performed. This type of work is generally called cron jobs.
The crontab file contains a series of jobs and instructions sent to the cron daemon. Each user can have their own crontab file, while the operating system holds a crontab file for the entire system, which is usually stored in subdirectories under/etc or/etc, and this file can only be modified by the system administrator.
Each line of the crontab file follows a specific format, separated by a space or tab into several realms, each of which can place a single or multiple numeric values.
Configure Cron Tasks
The main configuration file for Cron is/etc/crontab, which includes the following lines:
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root
home=/
# Run-parts
* * * * * Root run-parts/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily
4 * * 0 root run-parts/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly
The first four rows are variables that you can use to configure the Cron task runtime environment. The value of the shell variable tells the system which shell environment to use (in this case, the bash shell), and the path variable defines the paths used to execute the command. The output of the cron task is mailed to the user name defined by the mailto variable. Read more crontab related reading please click on the information www.neitui.me/zx/ If the MAILTO variable is defined as a blank string (mailto= ""), the e-mail message will not be sent. The home variable can be used to set the home directory to use when executing a command or script.
Each line in the/etc/crontab file represents a task, in the form of:
Minute hour day Month DayOfWeek command
minute-minutes, any integer from 0 to 59
hour-hours, any integer from 0 to 23
day-any integer from 1 to 31 (if a month is specified, it must be a valid date for that month)
Month-month, any integer from 1 to 12 (or use the English abbreviations of the month such as Jan, Feb, etc.)
Dayofweek-week, any integer from 0 to 7, where the 0 or 7 represents Sunday (or use the English abbreviations of the week such as Sun, Mon, etc.)
command-commands to execute (commands can be commands such as Ls/proc >>/tmp/proc, or commands to execute scripts you write yourself. )
In any of these values, an asterisk (*) can be used to represent all valid values. For example, an asterisk in a month value means that the command is executed monthly after other constraints have been met.
The short line between integers (-) specifies an integer range. For example, 1-4 means integers 1, 2, 3, 4.
Specify a list with a series of values separated by commas (,). For example, 3, 4, 6, 8 indicate these four specified integers.
A forward slash (/) can be used to specify the interval frequency. Adding/<integer> after a range means that integers can be skipped within the range. For example, 0-59/2 can be used to define every two minutes in the Minutes field. The interval frequency value can also be used with asterisks. For example, the value of */3 can be used in the month field to indicate that the task runs every three months.
The line starting with the pound sign (#) is a comment and will not be processed.
As you can see in the/etc/crontab file, it uses the Run-parts script to perform/etc/cron.hourly,/etc/cron.daily,/etc/cron.weekly, and/etc/cron.monthly Scripts in the directory that are executed hourly, daily, weekly, or monthly. ( internal push network www.neitui.me, focus on doing the Internet recruitment industry internal recommendations, to create the most professional Internet recruitment recommended Direct recruitment platform), the files in these directories should be shell scripts.
If a cron task needs to be executed according to the schedule instead of hourly, daily, weekly, or monthly execution, it can be added to the/ETC/CRON.D directory. All files in this directory use the same syntax as in/etc/crontab.
# Record the memory usage of the system every Monday
# at 3:30AM in the File/tmp/meminfo
3 * Mon cat/proc/meminfo >>/tmp/meminfo
# Run custom Script The first day of every month at 4:10AM
4 1 * */root/scripts/backup.sh
Crontab Command Introduction and cron task configuration