Linux System Task Dispatch command Crontab

Source: Internet
Author: User

Recurring Scheduled Tasks, scheduled execution tasks, such as this one, have cron commands on the Linux system to complete.

The task scheduling under Linux system is divided into two categories: System task scheduling and user task scheduling.

The/etc/crontab file is the configuration file for the System Task Scheduler:

[Email protected] ~]# Cat/etc/crontab
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root
home=/

# for details see Mans 4 Crontabs

# Example of Job definition:
#.----------------Minute (0-59)
# | .-------------Hour (0-23)
# |  | .----------Day of Month (1-31)
# |  |  | .-------month (1-12) OR jan,feb,mar,apr ...
# |  |  |  | .----Day of Week (0-6) (sunday=0 or 7) or Sun,mon,tue,wed,thu,fri,sat
# |  |  |  | |
# * * * * * * user-name command to be executed

[Email protected] ~]#

The first four rows are the environment variables that are configured to run the Crond task, the row shell variable specifies the use of/bin/bash, and the second line of the path variable specifies the path of the system execution command. The third line of the mailto variable specifies that cron's task execution information will be sent to the root user, and the fourth row home variable specifies the home directory to use when executing the command or script.

User Task scheduling: User Top up to perform the work, such as user data backup, timed email reminders and so on. Users can use the Crontab tool to customize their own scheduled tasks. All user-defined crontab files are saved in the/var/spool/cron/directory with the same file name and user name.

File:

/etc/cron.deny #该文件中所列用户不允许使用crontab命令

File:

/etc/cron.allow #该文件中所列用户允许使用crontab命令

File:

/var/spool/cron/#所有用户crontab文件存放的目录, named after a user name

Usage format for crontab: Minute hour day Month Week command

If minute is represented by *, then it represents every minute, preferably an integer between 0-59. One analogy.

command indicates which commands to execute, either a system command or a script file that you have written yourself.

In each of these fields, you can also use the following special characters:

Asterisk (*): represents all possible values, such as the month field if it is an asterisk, the command action is executed monthly after the constraints of other fields are met.

Comma (,): You can specify a list range with a comma-separated value, for example, "1,2,5,7,8,9"

Middle Bar (-): An integer range can be represented by a middle bar between integers, such as "2-6" for "2,3,4,5,6"

Forward slash (/): You can specify the interval frequency of the time with a forward slash, such as "0-23/2", which is performed every two hours. A forward slash can be used with asterisks, such as */10, if used in the minute field, which means that it executes every 10 minutes.

Crond services are integrated into the system, even if the installation of a minimal system, the service will be installed.

crontab command format: crontab [–u user] File

crontab [-u user] [–e] [–l] [–r] [–i]

Crontab–u Jack says to use Jack as the user to run the command

The-e parameter means "edit"-the meaning of the-l parameter is "view scheduled task"-the meaning of the-R parameter is "delete scheduled task" This parameter will delete all the almost tasks under the/var/spool/cron directory. The-i parameter means that the delete user's crontab file is given a confirmation prompt. Gives an interactive hint.

Example: 0 4 1 Jan */etc/int.d/smb restart #1月1号早上4点整重启smb服务.

* * * * * Root run-parts/etc/cron.hourly #每小时以root用户的身份执行 All scripts in the/etc/cron.hourly directory. Note: If you remove the Run-parts parameter, the directory must follow a specific script, not a folder.

Attention:

1. If we create a crontab file, but the task cannot be executed, it is not a problem to perform this task manually. In this case, it is generally caused by the absence of a configuration environment variable in the crontab file.

2. Pay attention to clean the system user's mail log, each task scheduled execution completed, the system will send the task output information through e-mail to the user, change information is generally stored in the/var/spool/mail directory. In this way, the log information in this directory can be very large, which may affect the normal operation of the system. Therefore, it is important to redirect each task after it is executed.

Example: 0 */3 * * */usr/local/apache2/apachectl restart >/dev/null 2>&1

>/dev/null means that the output information is purged, 2>&1 means that the standard error is redirected to the standard output, and the standard output is redirected to/dev/null, so both standard and standard errors are redirected to/dev/null.

In addition, the new cron job will not execute immediately, at least two minutes to execute, if the restart Cron will be executed immediately.

You can restart the Cron service when the crontab suddenly fails, or view the log: Tail-f/var/log/cron

Linux System Task Dispatch command Crontab

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.