In Linux, scheduled execution is mainly performed by adding a custom plan to the crontab file. The settings are slightly more complex than those in Windows (because there is no graphic interface), but they are not very complex, the key is to remember the/var/spool/cron directory. Let's take a look at the specific usage:
First, check the/etc/crontab file:
$ Cat/etc/crontab
SHELL =/bin/bash
PATH =/sbin:/bin:/usr/sbin:/usr/bin
MAILTO = root
HOME =/
# Run-parts
01 *** root run-parts/etc/cron. hourly
02 4 *** root run-parts/etc/cron. daily
22 4 ** 0 root run-parts/etc/cron. weekly
42 4 1 ** root run-parts/etc/cron. monthly
The first four rows are environment variables related to setting the cron task to run. The SHELL variable value specifies the SHELL environment used by the system (this example is bash shell). the PATH variable defines the PATH for executing commands. Cron output is sent to the username defined by the MAILTO variable by email. If the MAILTO variable is defined as a null string (MAILTO = ""), the email will not be sent. When executing commands or scripts, the HOME variable can be used to set the base directory.
The description format of each task in the/etc/crontab file is as follows:
Minute hour day month dayofweek command
Minute-integer from 0 to 59
Hour-integer from 0 to 23
Day-an integer from 1 to 31 (must be the valid date of the specified month)
Month-an integer from 1 to 12 (or a month abbreviated as Jan or Feb)
Dayofweek-an integer from 0 to 7. 0 or 7 is used to describe Sunday (or abbreviated as Sun or Mon)
Command-command to be executed (as ls/proc>/tmp/proc or command to execute custom scripts)
Root indicates running as root user
Run-parts indicates that a folder is followed by all the scripts in the folder.
For the preceding statements, asterisks (*) indicate all available values. For example, * indicates that the command is executed every month (other restrictions must be met.
The hyphen (-) between integers indicates an integer column. For example, 1-4 indicates an integer of 1, 2, 4.
The specified values are separated by commas. For example, 3, 4, 6, and 8 indicate the four specified integers.
The "/" symbol specifies the step setting. "/<Interger>" indicates the step value. For example, 0-59/2 is defined to be executed every two minutes. The step value can also be represented by an asterisk. For example, */3 is used to run a specified task every three months.
Comments starting with "#" are not executed.
If a cron task needs to be executed on a regular basis instead of by hour, day, week, or month, add the/etc/cron. d directory. All files in this directory have the same syntax as the/etc/crontab file. See the following example:
# Record the memory usage of the system every Monday
# At 3: 30 AM in the file/tmp/meminfo
30 3 ** mon cat/proc/meminfo>/tmp/meminfo
# Run custom scr pt the first day of every month at AM
10 4 1 **/root/scr restart pts/backup. sh
In addition to the root user, users can execute crontab configuration scheduler tasks. All user-defined crontabs are stored in the/var/spool/cron directory, and tasks are executed as creators. To create a crontab for a specific user, log on to the user and run the crontab-e command. The system starts editing crontab in the editing software specified in VISUAL or EDITOR. The file content is in the same format as/etc/crontab. Example:
0 3 ***/home/dbbackup/db1backup. sh backup
0 4 ***/home/dbbackup/db2backup. sh backup
Indicates that/home/dbbackup/db1backup. sh backup is executed at three o'clock every day, And/home/dbbackup/db2backup. sh backup is executed at four o'clock. If it is executed every five minutes, it can be changed:
*/5 */home/dbbackup/db2backup. sh backup
When the changed crontab needs to be saved, the file will be saved as the following file/var/spool/cron/username. The file name varies with the user name.
The cron Service Checks changes in the/etc/crontab,/etc/cron. d/,/var/spool/cron files every minute. If any change is found, it will be downloaded to the memory. Therefore, the program does not need to be restarted even if the crontab file changes. We recommend that you use the crontab-e command to add custom tasks. After exiting, use/etc/init. the d/crond restart command restarts the crond process. The official file says that you do not need to restart the process, but I cannot run the task without restarting it. At first, I don't know what the run-parts in the/etc/crontab file means. directly adding the command in the/etc/crontab format will always fail to run, later I learned that run-parts is followed by a folder.
For more details, please continue to read the highlights on the next page:
Use crontab in Linux to create scheduled tasks
Routine scheduling of crontab in Linux
Linux crontab does not run troubleshooting
Ubuntu uses crontab for scheduled tasks
Linux scheduled task (at batch crontab anacron)