Linux scheduled task execution

Source: Internet
Author: User

View the/etc/crontab file
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 about setting the environment variable for running the cron task. The shell variable value specifies the shell environment used by the system (this example is bash shell), and the path is changed
Defines the path for executing commands. The cron output is sent to the msilto variable volume user name in the form of an email. For example:
If the mailto variable is defined as a null string (mailto = ""), the home variable can be used to set the home directory when the email is not sent.
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 expressed by Sun or Mon in short)
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, the star number (*) indicates all available values. For example, * indicates that the month statement 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, tables 3, 4, 6, and 8 show 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/etc/cron. d directory. all files in this directory have the same syntax as the/etc/crontab file. view 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,
The task is executed as the creator. To create a crontab for a specific user, log on to the user and run the crontab-e command,
The system starts the editing software specified in visual or editor to edit crontab. The file content is in the same format as/etc/crontab. The example is as follows:
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.
*/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 will vary according to the user name.
The cron Service Checks/etc/crontab,/etc/cron every minute. d/, and changes under the/var/spool/cron file. 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. d/crond.
The restart command restarts the crond process. The official file says that the process does not need to be restarted, but I cannot run the task without restarting it. Do not know
What does run-parts mean? Simply add the command in the/etc/crontab format and it will never run. Later, I learned that run-parts is followed by folders.

Cron is a Linux scheduled execution tool that can run jobs without human intervention. Since cron is a built-in service in Linux, but it does not automatically get up, you can use the following methods to start and close this service:

/Sbin/service crond start // start the service
/Sbin/service crond stop // close the service
/Sbin/service crond restart // restart the service
/Sbin/service crond reload // reload the configuration

You can also enable the service automatically when the system starts:

/Sbin/service crond start

Now that the cron Service is in the process, we can use it. The Cron Service provides the following interfaces for you to use:

1. directly use the crontab command to edit

The cron Service provides the crontab command to set the cron service. The following are some parameters and descriptions of this command:

Crontab-u // set a user's cron service. Generally, the root user needs this parameter when executing this command.

Crontab-l // list the details of a user's cron Service

Crontab-r // Delete the cron service of no user

Crontab-E // edit a user's cron Service

For example, to view your cron settings as root: crontab-u root-l

For another example, Root wants to delete Fred's cron settings: crontab-u Fred-R

When editing the cron service, the edited content has some formats and conventions. Enter crontab-u root-e.

In VI editing mode, the edited content must conform to the following format: */1 ***** ls>/tmp/ls.txt

The first part of this format is the time setting, and the last part is the command to be executed. If there are too many commands to be executed, you can write these commands into a script, then you can directly call this script here.
When calling the command, remember to write the complete path of the command. We have a certain agreement on the time setting. The first five * numbers represent five numbers. The value range and meaning of the numbers are as follows:

Minutes (0-59)

Hour (0-23)

Date (1-31)

Month (1-12)

Week (0-6) // 0 represents Sunday

In addition to numbers, there are also several special symbols: "*", "/", "-", * representing all numbers in the value range, "/" indicates the meaning of each, "*/5" indicates every five units, "-" indicates the number from a number to a number, "," separate several discrete numbers. The following examples illustrate the problem:

Every morning

0 6 *** echo "Good morning. ">/tmp/test.txt // note that no output is visible from the screen with pure echo, because cron has emailed any output to the root mailbox.

Every two hours

0 */2 *** echo "have a break now.">/tmp/test.txt

Every two hours from PM to am, am

0 23-7/2, 8 *** echo "have a good dream :)">/tmp/test.txt

Am from Monday 4 to Wednesday every week

0 11 4*1-3 command line

Am, January 1, January 1

0 4 1 1 * command line

After you edit the cron settings of a user, cron automatically generates a file with the same name under/var/spool/cron. The Cron information of this user is recorded in this file.
File, you can only use crontab-e
. The cron reads the file every minute after it is started, and checks whether to execute the commands in it. Therefore, you do not need to restart the cron service after the file is modified.

2. Edit the configuration cron in the/etc/crontab file.

The cron service reads not only all files in/var/spool/cron every minute, but also/etc/crontab once. Therefore, we can use this file to configure
The cron service does something. Crontab configuration is intended for a user, and editing/etc/crontab is a system task. The file format of this file is:

Shell =/bin/bash
Path =/sbin:/bin:/usr/sbin:/usr/bin
Mailto = root // if an error occurs or data is output, the data is sent to this account as an email.
Home = // path of the user running. The root directory is used here.
# Run-Parts
01 *** root run-parts/etc/cron. Hourly // execute the script in/etc/cron. Hourly every hour
02 4 *** root run-parts/etc/cron. daily // run the script in/etc/cron. daily every day.
22 4 ** 0 root run-parts/etc/cron. Weekly // execute the script in/etc/cron. Weekly every week
42 4 1 ** root run-parts/etc/cron. Monthly // run the script in/etc/cron. Monthly every month.

Note the "run-parts" parameter. If this parameter is removed, you can write a script name to be run, instead of the folder name.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.