Linux script learning crontab command Learning

Source: Internet
Author: User
Tags ranges

Crontab is a program that conveniently executes a task in a scheduled (Cyclic) manner on unix/linux systems.

Crontab-e: edit the scheduled script;

Crontab-l view the timing script;

The cron service is a regular execution service. You can use the crontab command to add or edit tasks that require scheduled execution:

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

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

Edit the/etc/crontab file and add a line at the end: 30 5 * root init 6 so that the system is configured to automatically restart at every morning.

Basic usage:

1. crontab-l

List the current crontab task

2. crontab-d

Delete the current crontab task

3. crontab-e (crontab-r above solaris5.8)

Edit a crontab task and ctrl_D ends.

4. crontab filename

Use filename as the crontab task list file and load it

Crontab file format:

Rows in the crontab file are composed of six fields. Different fields are separated by spaces or the tab key. The first five fields specify the time when the command will be run.

Minutes (0-59)

Hour (0-23)

Date (1-31)

Month (1-12)

Day of the week (0-6, 0 indicates Sunday)

The 6th fields are strings to be executed at the appropriate time

Example:

# MIN HOUR DAY MONTH DAYOFWEEK COMMAND

#06:10 every morning

10 6 **** date

# Every two hours

0 */2 *** date (solaris 5.8 does not seem to support this method)

# Every two hours from PM to am, am

0 23-7/2, 8 **** date

# Am on the 4th day of each month and from Monday to Wednesday of each week

0 11 4 * mon-wed date

# A.m. of July

0 4 1 jan * date

Note: When Using crontab, the environment variables that can be accessed in the running script may not be consistent with those in the current test environment, it is safer to set the environment variable (export) in the running script program)

(1ndcreate a file named crond.txt as follows and restart it at 05:36 every morning.

36 5 * reboot

(2) upload to the/opt directory

(3) run the command

Crontab/opt/crond.txt

Crontab-l

Make the configuration file take effect: If the configuration file takes effect, you have to restart cron. Remember that since the cron configuration file under each user is modified. Restart the cron server.

In Fedora and Redhat, we should use;

[Root @ localhost ~] #/Etc/init. d/crond restart

If you want crond to run at startup, you should change its running level;

[Root @ localhost ~] # Chkconfig -- levels 35 crond on

Service crond status: Check the cron service status. If it is not started, service crond start starts it. The cron service is a scheduled service. You can use the crontab command to add or edit tasks that require scheduled execution.

 

 

Each row of the Crontab file is composed of six fields (minutes, hours, day of month, month, day of week, and command). The fields are separated by spaces or tabs:

Minutes: minute field. The value ranges from 0 to 59.

Hours: small time domain. The value ranges from 0 to 23.

Day of month: date. The value ranges from 1 to 31.

Month: month. The value ranges from 1 to 12.

Day of week: day of the week. The value ranges from 0 to 6. The value of Sunday is 0.

Command: the command to be run

If a domain is *, the command can be executed within all possible values of the domain.

If a field is two numbers separated by a hyphen, the command can be executed within the range of two numbers (including the two numbers themselves ).

If a field is composed of a series of values separated by commas, the command can be executed within the range of these values.

If both the date and week fields have values, these two fields are valid.

Write a file to start the automatic backup process.

 

CD/OPT

Touch reboot.txt

Add the following content to reboot.txt:

0 4 *** reboot

 

Crontab/opt/reboot.txt

Use crontab-e to edit scheduled operations. For example, add the following command:

Use the crontab-l command to view

 

Note: You need to start the service (added to RC. Local)

 

Restart a crond task

 

/Etc/init. d/cron restart (under Ubuntu)

 

First, in systems managed by rpm packages such as fedora or RedHat;

 

[Root @ localhost ~] #/Etc/init. d/crond start

[Root @ localhost ~] #/Etc/init. d/crond stop

[Root @ localhost ~] #/Etc/init. d/crond restart

 

/Etc/rc. d/init. d/crond restart

 

Command Overview

 

Crontab-operate the daemon of each user and the execution schedule.

 

Some parameter descriptions

Crontab file [-u user]-replace the current crontab with the specified file.

Crontab-[-u user]-replace the current crontab with the standard input.

Crontab-1 [user]-list the current crontab of the user.

Crontab-e [user]-edit the current crontab of the user.

Crontab-d [user]-delete the current crontab of the user.

Crontab-c dir-specifies the crontab directory.

Crontab file format: m h d m d cmd.

M: minute (0-59 ).

H: hour (0-23 ).

D: Day (1-31 ).

M: Month (1-12 ).

D: days in a week (0 ~ 6, 0 is Sunday ).

The program to be run by cmd is sent to sh for execution. The shell contains only three environment variables: USER, HOME, and SHELL.

The following is an example file:

 

 

# Min hour day month dayofweek command #106 * date at every DAY #0 */2 * date every two hours # every two hours from PM to AM, am,-7/2, 8 * ** date #4 of each month and 11: 0 11 a.m. from Monday to wed date of every week # a.m. of February 4: 0 4 1 jan * date example lark: ~> The crontab-1 lists the current crontab of the user. # min hour day month dayofweek command 10 6 * date 0 */2 * date 0 23-7/2, 8 * date lark: ~>

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.