Basic usage of crontab

Source: Internet
Author: User
Crond resident command for task scheduling
Crond is a command that Linux uses to execute programs on a regular basis. When the operating system is installed, the task Scheduling command is started by default. The Crond order will periodically check whether there is any work to be done and will automatically carry out the work if there is any work to be done. and Linux task scheduling is mainly divided into the following two categories:
1, the system implementation of the work: the system periodically to perform the work, such as backup system data, clean cache
2, Personal work: a user to do the work on a regular basis, such as every 10 minutes to check the mail server for new letters, these work can be set by each user


Cron Usage Instructions

Cron's usage is always not remember, simply write down the memo. Much of what follows is translated from the Cron help Guide, some of which are added by themselves.

The full text reads as follows:

Cron comes from the Greek word chronos (meaning "time") and is the next program in the Linux system that automatically executes the specified task. For example, if you want to create a backup of some files or folders during your nightly sleep, you can use cron to move from execution. start and stop of services

The cron service is a built-in service for Linux, but it does not boot automatically. You can start and stop a service with the following command:

/sbin/service Crond Start
/sbin/service Crond Stop
/sbin/service Crond Restart
/sbin/service Crond Reload

The above 1-4 lines are start, stop, restart service, and reload the configuration respectively.

To set up a cron to start automatically at boot time, add/sbin/service crond start to the/etc/rc.d/rc.local script. view, edit, and delete

Cron stores the command line in a crontab (cron table) file, which is usually in the/etc directory. Each system user can have its own crontab (under/var/spool/cron/). To view the current user's crontab, enter Crontab-l, edit the crontab, enter CRONTAB-E, delete crontab, and enter Crontab-r. If you are currently root, to view/edit/delete/crontab a user, simply add-u USERNAME (such as crontab-e-U USERNAME) after the corresponding command. The default editor for crontab files is VI, you can enter export visual= ' editor ' to change the default editor.

The Cron service will not only read all the files in the/var/spool/cron directory once per minute, but also read the/etc/crontab files once. Configuring this file also allows Cron to perform tasks. Using the crontab command is a configuration of user-level tasks, while editing/etc/crontab files is a configuration of system-level tasks.

Crontab is a timed task trigger under a UNIX system whose user permissions are recorded in the following two files:

File

Meaning

/etc/cron.deny

The user listed in this file is not allowed to use the crontab command

/etc/cron.allow

Users listed in this file are allowed to use the crontab command

/var/spool/cron/

is the crontab file for all users

/var/spool/cron/crontabs
/var/spool/cron/crontabs
The format of the crontab command is: crontab–l|-r|-e|-i [username], whose parameter meaning is as table one:

Parameter name

Meaning

Example

-L

Displays the contents of the user's crontab file

Crontabl–l

-I.

to prompt before deleting a user's crontab file

Crontabl-ri

-R

Remove a user's crontab file from the crontab directory

Crontabl-r

-E

Edit a user's crontab file

Crontabl-e

The crontab file created by the user is stored in/var/spool/cron and has the same filename as the user name.
Its format is divided into six paragraphs, the first five paragraphs for the time set paragraph, the sixth paragraph is the command segment to be executed,
The format is as follows: * * * * * *
The meaning of its time period is as table two:

Paragraph

Meaning

Take value range

First paragraph

Representative minutes

0-59

Second paragraph

Representative hours

0-23

Third paragraph

Representative date

1-31

Fourth paragraph

Representative month

1-12

Fifth paragraph

On behalf of the day of the week, 0 for Sunday

0-6


Name: crontab
Use Rights: All users
How to use:
crontab [-u user] File
crontab [-u user] {-l |-r | e}
Description
Crontab is used to allow the user to execute a program at a fixed time or at a fixed interval, in other words, a user-like schedule. -u user is the time table that specifies the user, as long as you have permission (for example, root) to specify the schedule for others. If you do not use-u user, it means setting your own schedule.
Number of meals:
-E: Execute the text editor to set the time table, the default text editor is VI, if you want to use another text editor, please set the VISUAL environment variables to specify the use of the text editor (for example, Setenv VISUAL Joe)
-R: Deletes the current schedule table
-L: List the current schedule
The format of the schedule table is as follows:
F1 F2 F3 f4 f5 program

Where F1 is the minute, F2 represents the Hour, F3 represents the day of the month, the F4 represents the month, and the F5 represents the day of the one week. Program represents the programs to be executed.
When F1 is *, it means that the PROGRAM,F2 is executed every minute for *, and the rest of the program is executed every hour.
When F1 is a-b to be executed from the time of the first a minute to the B minute, the F2 is a-b to be performed from A to a B-hour, and the remainder of the analogy
When F1 is */n, it is executed once every n minutes, F2 is performed once per N-hour intervals for */n, and the rest
When F1 is a, B, C,... The first A, B, C,... Minutes to execute, F2 for a, B, C,... The first is a, B, c ... An hour to execute, and the rest of the analogy
The user can also store all the settings in the file file, using crontab file to set the schedule.
Example:
Perform a/bin/ls every 0 minutes per day of the month:
0 7 * * */BIN/LS

In December, every 6 to 12 o ' Day in the morning, every 20 minutes to perform a/usr/bin/backup:
0 6-12/3 */usr/bin/backup

Send a letter to Alex@domain.name from Monday to Friday every 5:00:
0 * * * 1-5 mail-s "HI" alex@domain.name/dev/null 2>&1 can
Example: If the contents of the user's crontab file are: * * * * echo its dinner time, the system's 19:29 per day displays ' its dinner time '
Example (create a cron full process that will enter the current time in test.txt every minute):

1. Log on to the Linux system as a regular user (I'm using CentOS4.1)

2. $crontab –e
Description: The system Default editor is vim, if not please add the following shell:
$EDITOR =vi
$export EDITOR

3. Enter "*/1 * * * * Date >> $HOME/test.txt", Save and exit VIM

4. $su Root

5. $CD/ETC/INIT.D

6./crond Restart

Let's take a look at a few specific examples:
0 */2 * * * */sbin/service httpd restart means to restart Apache every two hours

7 * * * */sbin/service sshd start with SSH service 7:50 every day

* * * * */sbin/service sshd stop means shut down SSH service 22:50 every day

0 0 1,15 * * fsck/home 1th and 15th monthly check/home disk

1 * * * * * */home/bruce/backup The first part of the hour to execute/home/bruce/backup this file

* * 1-5 find/home "*.xxx"-mtime +4-exec rm {} \; Every Monday to Friday 3 o'clock, in the directory/home, locate the file named *.xxx and delete the file 4 days ago.

6 */10 * * ls means 1, 11, 21, 31st of every month Yes 6:30 execute the LS command once








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.