Crontab is used to set the instances of periodically executed commands.

Source: Internet
Author: User
Tags cron script

A simple instance used by crontab

1) create a file in any directory of ubuntu

# Touch cronfile01

2) EDIT crontab file01 [note]: If the root user uses crontab-e to write the root file in the/etc/spool/cron/crontabs directory, this file is written by default, after you log on with the root user and use crontab file01, the pre-scheduled policy in the root file will be overwritten by file01.

# Vi cronfile01

*/2 ***** echo "Haha"

Indicates that Haha is output every 2 minutes.

3) Submit cornfile01 to the cron Process

# Crontab cronfile01

4) use crontab-L to list crontab Files

# Crontab-l

5) To view the execution result of the crontab file, redirect the output result of confile01 to a file.

# Vi cronfile01

*/2 ***** echo "Haha">/data/test.txt

In this example, a Haha file can be written to the test.txt file every 2 minutes.

# Less test.txt

 

 

 

 

 

 

 

 

Crontab commands are common in UNIX and Unix-like operating systems and are used to set periodically executed commands. This command reads commands from the standard input device and stores them in the crontab file for later reading and execution. The word is from Greek.
Chronos.

Generally, the commands stored in crontab are activated by the daemon, and crond is often run in the background. check whether a scheduled job needs to be executed every minute. This type of job is generally called cron jobs.

The crontab file contains a series of jobs and commands sent to the cron daemon. Each user can have their own crontab file. At the same time, the operating system saves a crontab file for the entire system, which is usually stored in subdirectories under/etc or/etc, this file can only be modified by the system administrator.

Crontab is used to allow users to execute programs at a fixed time or interval. In other words, it is similar to the user's time table. -U user is used to set the time table of the specified user. The premise is that you must have the permission (for example, root) to specify the time table of another user. If-u user is not used, the time table is set.

Parameters:

-E [username]: run the text editor to set the time table. The preset text editor is vi. If you want to use another text editor, set the visual environment variable to specify the Text Editor (for example, setenv visual Joe)

-R [username]: deletes the current time table.

-L [username]: list the current time series table

-V [username]: lists the statuses of user cron jobs.

The time table format is as follows::

F1 F2 F3 F4 F5 Program

F1 indicates the minute, F2 indicates the hour, F3 indicates the day of the month, F4 indicates the month, and F5 indicates the day of the week. Program indicates the program to be executed.

When F1 is *, the program is executed every minute, when F2 is *, the program is executed every hour, and so on.

When F1 is a-B, it indicates that execution is performed from the minute a to the minute B. When F2 is a-B, it indicates that execution is performed from the hour a to the hour B, other analogy

When F1 is */N, the execution is performed every n minutes. If F2 is */N, the execution is performed every n hours.

When F1 is a, B, c ,... a, B, C ,... execute in minutes. F2 is a, B, c ,... a, B, c... execute in hours, and so on

Usage:

Use VI to edit a cronfile file, and then enter a time table in good format in the file. After editing, save and exit.

Enter

$: Crontab cronfile

In this way, the cronfile file is submitted to the c r o n process, A copy of the newly created cronfile has been stored in the/v a r/s p o l/c r o n directory. The file name is the user name.

  Example:

Run the command/bin/ls once every 0th minutes of every hour every month:

0 ***/bin/ls

During October 11, December, execute/usr/bin/backup every 20 minutes from to every day:

*/20 6-12*12 */usr/bin/backup

Send a letter to alex_mail_name at pm from Monday to Friday:

0 17 ** 1-5 mail-s "hi" alex_mail_name </tmp/maildata

Execute echo "Haha" at midnight, 00:20, and 02:20 every month"

20 0-23/2 *** echo "Haha"

Every two hours from PM to am, am

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

Create crontab?

Before submitting a crontab file to the cron process, you must set the environment variable editor. The cron process determines which editor to use to edit the crontab file. 99% of UNIX and Linux users use VI. If so, edit the. profile file in the $ home directory and add the following line to it:

Editor = VI; export Editor

Save and exit.

Create a file named <user> Cron, where <user> is the user name. To submit the crontab file you just created, you can use the newly created file as the parameter of the cron command:

$ Crontab davecron

Now the file has been submitted to the cron process. At the same time, a copy of the newly created file has been stored in the/var/spool/cron directory. The file name is the user name (Dave ).

In Linux, Cron, ATD, and anacron are three important daemon processes responsible for scheduling various background jobs that run regularly. Implement background Job Scheduling and regularly execute various system tasks and user programs, including repetitive tasks and one-time jobs. In terms of system tasks and applications executed during scheduling, Cron, ATD, and anacron Daemon have their respective focuses and division of labor. Cron is the main job scheduling program in Linux and is responsible for scheduling and executing all system tasks. atd is responsible for scheduling and executing scheduled jobs submitted by users using at (or batch) commands; anacron is a supplement to cron. If there is a cron job that should be run but not run during the downtime, anacron will assist in scheduling and execution after the system is started.

By using the background job scheduling mechanism, you can also submit your own background jobs so that you can periodically and automatically execute your own applications (including system commands and shell scripts ). For example, perform system backup at a certain time every night or on a weekly weekend, and collect system data at a certain interval.

The crontab file is mainly used to submit repeated tasks. The at command is only used to submit one-time tasks.

The main function of the cron daemon is to manage and schedule background jobs submitted in the form of a crontab file, and automatically execute the specified command according to the specified date and time.

During system startup, the cron daemon is automatically started in Linux by executing the cron script in the/etc/init. d directory. Once started, the cron daemon first checks whether the/var/spool/cron/crontabs directory contains a user-defined crontab file named after the user name. Then, check the/etc/crontab file and check whether the system crontab file exists in the/etc/cron. d directory. If the cron daemon exists, the cron daemon executes the following tasks:

1) read and load the tasks defined in the crontab file, including the execution time and corresponding commands (or shell scripts );

2) run every minute and check every task that has been loaded into the memory. If the specified time of a task matches the current system time, the task is scheduled to run the corresponding command or shell script;

3) when executing a specified command or shell script, any output information will be sent to the owner of the crontab file by email, or the user specified by the mailto environment variable in the crontab file;

4) check whether the modification time of the/var/spool/cron/crontabs and/EC/cron. d directories changes after the/etc/crontab file is automatically loaded. If there is any change, cron will examine all the crontab files in the preceding two directories and reload the edited or modified crontab files.

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.