Use of the crontab command for CentOS scheduled tasks

Source: Internet
Author: User

Use of the crontab command for CentOS scheduled tasks
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.

How to Use
  1. First view help
[root@dmp004 orderreport]# crontab -husage:  crontab [-u user] file    crontab [-u user] [ -e | -l | -r ]        (default operation is replace, per 1003.2)    -e  (edit user's crontab)    -l  (list user's crontab)    -r  (delete user's crontab)    -i  (prompt before deleting user's crontab)    -s  (selinux context)

The usage format is as follows:

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

-U followed by user name

-E Indicates editing the scheduled task list.

-L list user scheduled tasks

File is the file that defines the scheduled task.

Other parameters are clear at a glance. The file format is very important.

minute hour day month week command

The file contains six fields, the first five fields are time, and the last field is the command to be executed.

Minute indicates the minute, which can be 0-59
Hour indicates the hour, which can be 0-23
Day indicates the day, which can be 1-31
Month indicates month, which can be 1-12
Week indicates the number of weeks, which can be 0-7
Command indicates the command to be executed. It can be a linux command or a shell script.

In the time field:
Asterisk * represents any time
Comma (,). For example, if hour is "3, 4, 9", it indicates that the command must be executed at, and.
Horizontal Bar-indicates that the value range is "3-9", for example, from.
Diagonal lines \/indicate the time interval. For example, if hour is "0-23/2", it indicates that it is executed between and every two hours.

Common
  1. View scheduled tasks of the current user
[root@dmp004 orderreport]# crontab -l50 17 * * * /home/mofei/task/orderreport/start.sh
  1. View scheduled tasks under User root
[root@dmp004 orderreport]# crontab -u root -l50 17 * * * /home/mofei/task/orderreport/start.sh

3. Add a scheduled task to the root user
Define test. task as follows:

* * * * * echo 'this is a test'

Add to scheduled task list

[root@dmp004 orderreport]# crontab -u root test.task[root@dmp004 orderreport]# crontab -u root -l* * * * * echo 'this is a test'
  1. Delete the root scheduled task
[root@dmp004 orderreport]# crontab -u root -r

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.