Linux scheduled task crontab

Source: Internet
Author: User

Linux scheduled task crontab

 

Cron is a built-in service of Linux, but it does not automatically get up. You can start or close this service using the following methods:

Reference:

/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

 

// ================================================ ==========================================

 

# Use the hash sign to prefix a comment

# + ---------------- Minute (0-59)

# | + ------------- Hour (0-23)

# | + ---------- Day of month (1-31)

# | + ------- Month (1-12)

# | + ---- Day of week (0-7) (Sunday = 0 or 7)

# |

# ***** Command to be executed

 

// ================================================ ========================================================

 

 

In many cases, you cannot restart crond. In this case, you can killall crond first and then crond restart.

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

Reference:

Add:

/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:

Reference:

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:

Reference:

Crontab-u root-l

For example, Root wants to delete Fred's cron settings:

Reference:

Crontab-u Fred-R

When editing the cron service, the edited content has some formats and conventions. Enter:

Reference:

Crontab-u root-e

In the VI editing mode, the edited content must conform to the following format:

Reference:

*/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. Remember to write the complete path of the command during the call. 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:

Reference:

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:

Reference:

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 "Welcome to http://beyl.cn.">/tmp/test.txt

Every two hours from PM to am, am

0 23-7/2, 8 *** echo "Welcome to http://beyl.cn. :)">/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 a user's cron settings are edited, cron automatically generates a file with the same name under/var/spool/cron, the cron information of this user is recorded in this file. This file cannot be edited directly. You can use crontab-e to edit it. 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 not only reads all files in/var/spool/cron every minute, but also reads/etc/crontab once. Therefore, we can use the cron service to configure this file. Crontab configuration is intended for a user, and editing/etc/crontab is a system task. The file format of this file is:

Reference:

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 =/

# Run-Parts

01 *** root run-parts/etc/cron. Hourly // run the script in/etc/cron. Hourly every hour.

02 4 *** root run-parts/etc/cron. daily // run the script in/etc/cron. daily once a day.

22 4 ** 0 root run-parts/etc/cron. Weekly // run the script in/etc/cron. Weekly once a week.

42 4 1 ** root run-parts/etc/cron. Monthly // run the script in/etc/cron. Monthly every month.

User running path

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

Cron

Scheduled execution command (cron ):

Crontab [*/minute] [*/hour] [*/day] [*/month] [* (/dayofweek)?] Command

Minute: minute, 1 ~ 59

Hour: hour, 0 ~ 23

Day: date, 1 ~ 31

Month: month, 1 ~ 12 or Jan, Feb...

Dayofweek? : 0 (Sunday )~ 6 (Saturday), or Mon, Tue...

Command: the command to be executed. Multiple commands are separated by; in the middle.

 

-E: edit the file/var/spool/cron/crontabs/username. Starting with # indicates the annotation.

-L: list the content of the/var/spool/cron/crontabs/username file.

-D: Delete the user's work schedule.

-R: deletes the user's work schedule.

 

Minute, houre, day, month, dayofweek? It is a conditional condition. The command is executed only when all conditions are met.

Use * to represent skipping this condition.

If a field has multiple conditions, separate them with commas.

If it refers to a continuous period of time, the center is separated.

If you want to execute at a fixed time, use/#, which means to execute.

 

Crontab command. The default value is/bin/sh, and the user's home directory is the working directory. However, you can use the home, shell, and PATH variables to change the literal interpreter, preset execution directory, and path during execution. You can also set mailto to set the e-mail address for the record after execution.

# Use/bin/sh to run commands, No matter what/etc/passwd says

Shell =/bin/sh

# Mail any output to 'Paul ', no matter whose crontab this is

Mailto = Paul

#

# Run five minutes after midnight, every day

5 0 * ** $ home/bin/daily. Job> $ home/tmp/out 2> & 1

# Run at pm on the first of every month -- output mailed to Paul

15 14 1 ** $ home/bin/monthly

# Run at 10 pm on weekdays, annoy Joe

0 22 ** 1-5 mail-s "it's 10 PM" Joe % Joe, % Where are your kids? %

23 0-23/2 *** echo "run 23 minutes after midn, 2 am, 4 am..., everyday"

5 4 ** sun echo "run at 5 after 4 every Sunday"

Root can use-u user name to edit crontab settings of other users.

Crontab-u username-e

 

At startup, the RC file will load the crond daemon,

It regularly reads the file and executes commands according to its content.

The command execution result (standard output/error output, stdout/stderr) will be sent to the system administrator by email,

That is, root: username in the/etc/aliases file.

 

You can also save commands as files and then use

Crontab filename

To write the command to the/var/spool/cron/crontabs/username file.

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: info-contact@alibabacloud.com 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.