Linux System Management (i): Timed Task crontab command

Source: Internet
Author: User

Objective:


We know that at command is a user directly given a point in time to perform a specific task, for some of the daily needs to execute the command, we can not go every day to execute once, so, Linux provides a loop run routine work command "crontab", it is by cron (Crond) This system service to control, it OPS personnel by configuring the Crontab configuration file to realize the loop operation, currently it is the Internet is very common technology. (We can also think of it as "Task Scheduler" under Windows), and then we'll look at how to use this command:


The syntax of crontab

[[Email protected] ~]# crontab [-u username] [-l|-e|-r] options and Parameters:-u  :   Only  root  can do this task, to help other users create/remove  crontab  work tasks-e  :  edit  crontab  Task-l  :  View  crontab  Tasks-r  :  Remove all  crontab  tasks, if you want to remove only one item, use  -e  to edit. Example: Write "This is only for crontab test" to the/tmp/output.txt file every two minutes [[email protected]  ~]# crontab -e*/2 * * * * echo  "This is only for  crontab test " >> /tmp/output.txt execution results [[email protected] ~]# ls - Lh /tmp/output.txt-rw-r--r--.  1 root root 30 jan 14 17:46 /tmp/ Output.txt[[email protected] ~]# ls -lh /tmp/output.txt-rw-r--r--.  1 root  root 60 jan 14 17:48 /tmp/output.txt[[email protected] ~]# cat  /tmp/Output.txtthis is only for crontab testthis is only for crontab  test Execution Log query:  where the log is stored:/var/log/cron[[email protected] log]# cat cron |  tail 3jan 14 17:46:01 localhost crond[4975]:  (Root)  CMD  (echo  " This is only for crontab test " >> /tmp/output.txt ) jan 14  17:48:02 localhost CROND[5023]:  (Root)  CMD  (echo  "this is only  for crontab test " >> /tmp/output.txt ) jan 14 17:50:01  localhost crond[5070]:  (Root)  CMD  (echo  "This is only for crontab  test " >>

Use of some special characters

Comma (,) example: Daily 6 o'clock in the afternoon with 8-point backup/etc/directory to/backup/under 0 6,8 * * * cp/etc/backup/
Minus sign (-) Example: daily 9 to 12 hourly 30-minute backup/etc/datamakr to/BACKUP/LOG/30 9-12 * * * cp/etc/datamakr/backup/log/

Slash (\ n) Example: Every minute, as in the example above */2 every two minutes to execute a command

Restrictions on the use of CRONTAB commands


/etc/cron.allow: will be able to use the Crontab account can be written to it, if not in this file users are not allowed to use crontab;

/etc/cron.deny: Will not be able to use the Crontab account to write to it, if not recorded in this file users, you can use the crontab.

in order of priority,/etc/cron.allow than/etc/cron.deny priority, and judge above, these two files only choose one to limit it, therefore, it is recommended that you just keep one, so as not to affect their configuration above the judgment! In general, the system is reserved by default/etc/cron.deny, you can not want to let him run crontab of the user to write/etc/cron.deny, an account line! The command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution.


Crontab configuration file


First, let's take a look at crontab configuration file contents

[[email protected] ~]# cat /etc/crontabshell=/bin/bash    # Shell file used by path=/sbin:/bin:/usr/sbin:/usr/bin    #命令的搜索路径MAILTO =root   # To whom the results are forwarded by mail, the default is admin root (or the email address) # for details see man 4 crontabs#  EXAMPLE OF JOB DEFINITION:#&NBSP:---------------- minute  (0 - 59) # |  &NBSP:------------- hour  (0 - 23) # |  |  .----------  day of month  (1 - 31) # |  |  |  .-------  month  (1 - 12)  OR jan,feb,mar,apr ...# |  |  |   | &NBSP:---- day of week  (0 - 6)   (sunday=0 or 7)  or sun,mon,tue,wed,thu,fri,sat# |  |  |  |  |# *   *  *  *   * user-name  command to be executed 

remark: Why is the command we executed above using "CRONTAB-E" not saved in this file? The reason is that "crontab-e" is designed for the user's cron, if you want some daily need to be able to cycle, then directly edit "/etc/crontab" This file, cron will read every minute/etc/crontab and/var/spool /cron the contents of the data inside, so as long as you finish editing/etc/crontab this file, and save him, then the cron configuration will automatically run.


options Minutes Hour Day-of-month month of year day of week command
Significance Minutes Hours Date Month Week Command
Range of values 0-59 0-23 1-31 1-12 0-6,0,7 on behalf of Sunday


Status view of the cron service process

CentOS 7:systemctl status Crondcentos 6:service crond status

Restart

/etc/init.d/crond restart


Reference documents:

http://vbird.dic.ksu.edu.tw/linux_basic/0430cron_3.php

http://www.php-note.com/article/detail/786


This article is from the "near home" blog, be sure to keep this source http://nearlv.blog.51cto.com/2432295/1735243

Linux System Management (i): Timed Task crontab command

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.