Linux crontab command usage details and examples, linuxcrontab

Source: Internet
Author: User

Linux crontab command usage details and examples, linuxcrontab

Linux systems are controlled by the cron (crond) System Service. The Linux system has a lot of planned work, so this system service is started by default. In addition, because users can also set scheduled tasks themselves, the Linux system also provides the command for users to control scheduled tasks: crontab command [@ more @]

1. crond Introduction

Crond is a daemon in Linux that is used to periodically execute a task or wait for processing some events. It is similar to a scheduled task in windows. After the operating system is installed, by default, the service tool is installed and the crond process is automatically started. The crond Process regularly checks whether there are tasks to be executed every minute. If there are tasks to be executed, the task is automatically executed.

In Linux, task scheduling is divided into two types: System Task Scheduling and user task scheduling.

System Task Scheduling: the tasks that the system periodically performs, such as writing cached data to the hard disk and clearing logs.There is a crontab file in the/etc directory, which is the configuration file for system task scheduling..

The/etc/crontab file contains the following lines:

[Root @ localhost ~] #Cat/etc/crontab

SHELL =/bin/bash

PATH =/sbin:/bin:/usr/sbin:/usr/bin

MAILTO = "" HOME =/

# Run-parts

51 * root run-parts/etc/cron. hourly

24 7 * root run-parts/etc/cron. daily

22 4 ** 0 root run-parts/etc/cron. weekly

42 4 1 ** root run-parts/etc/cron. monthly

[Root @ localhost ~] #

The first four rows are used to configure the environment variables for running crond tasks. The first line of SHELL variables specifies the shell to be used by the system. bash is used here, the PATH variable in the second line specifies the PATH for the system to execute the command. The MAILTO variable in the third line specifies that the crond task execution information will be sent to the root user by email. If the value of MAILTO variable is null, the task execution information is not sent to the user. The HOME variable in the fourth line specifies the main directory used for executing commands or scripts. The meaning of the lines 6 to 9 will be detailed in the next section. I will not talk about it here.

User task scheduling: jobs that you regularly perform, such as user data backup and scheduled email reminders. You can use the crontab tool to customize your own scheduled tasks. All user-defined crontab files are saved in the/var/spool/cron directory. The file name is the same as the user name.

User permission file:

File:

/Etc/cron. deny

Note:

Users listed in this file cannot use the crontab command.

File:

/Etc/cron. allow

Note:

Users listed in this file can use the crontab command

File:

/Var/spool/cron/

Note:

The directory where all user crontab files are stored, named after the user name

Meaning of the crontab file:

In the crontab file created by the user, each row represents a task, and each field in each row represents a setting. Its format is divided into six fields. The first five fields are time sets, the sixth part is the command segment to be executed. The format is as follows:

Minute hour day month weekcommand

Where:

Minute: minute. It can be any integer from 0 to 59.

Hour: indicates the hour, which can be any integer from 0 to 23.

Day: indicates the date, which can be any integer from 1 to 31.

Month: represents the month, which can be any integer from 1 to 12.

Week: the day of the week. It can be any integer from 0 to 7. Here 0 or 7 represents Sunday.

Command: the command to be executed. It can be a system command or a script file compiled by yourself.

You can also use the following special characters in the preceding fields:

Asterisk (*): represents all possible values. For example, if the month field is an asterisk, this command is executed every month after the conditions of other fields are met.

Comma (,): values separated by commas (,) can be used to specify a list range, for example"

Middle bars (-): You can use the middle bars between Integers to represent an integer range. For example, "2-6" indicates "2, 3, 4, 5, 6"

Forward slash (/): You can use a forward slash to specify the interval of time. For example, "0-23/2" indicates that execution is performed every two hours. At the same time, the forward slash can be used with the star number, for example, */10. If it is used in the minute field, it indicates that the execution is performed every ten minutes.

Ii. crond Service

Install crontab:

Yum install crontabs

Service operation instructions:

/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

View the crontab service status:

Service crond status

Start the crontab service manually:

Service crond start

Run the following command to check whether the crontab service is set to start at startup:

Ntsysv

Add boot auto start:

Chkconfig-level 35 crond on

Iii. crontab command details

1. Command Format:

Crontab [-u user] file

Crontab [-u user] [-e |-l |-r]

2. command functions:

Through the crontab command, we can execute the specified system command or shell script at a fixed interval. The time interval can be any combination of minutes, hours, days, months, weeks, and above. This command includes non-permanent and periodic log analysis or data backup.

3. command parameters:

-U user: used to set the crontab service for a user. For example, "-u ixdba" indicates to set the crontab service for the ixdba user. This parameter is generally run by the root user.

File:File is the name of the command file. It indicates that file is used as the task list file of crontab and is loaded into crontab.. If this file is not specified in the command line, the crontab command accepts the commands typed on the standard input (keyboard) and loads them into the crontab.

-E: edit the contents of a user's crontab file. If no user is specified, the crontab file of the current user is edited.

-L: displays the contents of a user's crontab file. If no user is specified, the contents of the current user's crontab file are displayed.

-R: deletes the crontab file of a user from the/var/spool/cron directory. If no user is specified, the crontab file of the current user is deleted by default.

-I: A confirmation prompt is displayed when the user's crontab file is deleted.

4. Common Methods:

1) Create a New crontab file

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. 9 9% 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 Cron file, where Is the user name, for example, davecron. Add the following content to the file.

# (Put your own initials here) echo the date to the console every

#15 minutes between 6 and 6 am

0, 15, 30, 45 18-06 ***/bin/echo 'date'>/dev/console

Save and exit. Make sure that the first five fields are separated by spaces.

In the preceding example, the system outputs the current time to the console every 5 minutes. If the system crashes or hangs, you can see at the last displayed time when the system stops working. In some systems, tty1 is used to represent the console. You can modify the preceding example based on the actual situation. To submit the crontab file you just created, you can use the newly created file as a parameter of the cron command:

$ Crontab davecron

Now the file has been submitted to the cron process and will run every 5 minutes.

At the same time, a copy of the new file has been stored in the/var/spool/cron directory, and the file name is the user name (dave ).

Related Article

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.