Detailed unix/linux the cycle execution instruction crontab command _unix Linux

Source: Internet
Author: User
Tags time interval

Brief introduction

The crontab command is common in UNIX and unix-based operating systems, and is used to set periodically executed instructions. This command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution.

Typically, crontab stored instructions are activated by the daemon, Crond often run in the background, checking every minute for scheduled jobs to be performed. This type of assignment is commonly referred to as cron jobs.

Cron is a service in Unix/linux that provides regular execution of shell commands, including the Crond and crontab parts:

Crond:cron Service Daemon, resident memory responsible for regular scheduling

Crontab:cron Management tool, responsible for editing schedule

The following demo is performed under Ubuntu 16.04. The basic use method can be viewed with commands man crontab

NAME
  crontab-maintain crontab files for individual users (Vixie Cron)

Synopsis
  crontab [-u user] File
  crontab [-u user] [i] {-e |-l |-r}

To explain briefly

-eedit, like vim, check syntax when saving exit

-l Enumerate All Tasks

-rDelete All Tasks

If crontab runs wrong, you can view the log file/var/log/syslog

Basic syntax

The cron syntax is very simple, with a total of six chunks, the first five are used to specify a time period, and the last piece is a specific command to execute, which looks like a format:

Min Hour day Month Week command

which

min minutes, Range 0-59

hour hours, Range 0-23

day Day, Range 1-31

L can be filled in to indicate the last day of the month

You can fill in the w,1w to indicate the most recent working day from No. 1th

month Month, Range 1-12

The last day of the month crontab itself is not supported and needs to be judged by scripting

week Week, Range 0-7

Here 0 and 7 all mean Sunday

Week and Moon can not coexist, may conflict

You can fill out #,4#3 for the third Thursday of the month

You can fill in l,5l for the last Friday of the month

command represents the specific command to execute (preferably an absolute path)

If you have more than one command, you need to connect with &, or write multiple commands in a shell script, and then crontab execute the shell script periodically

In addition, similar to regular expressions, there are some special symbols to help us achieve flexible scheduling

*asterisk, which means that each possible value is accepted

For example, * * * * command means that command is executed every minute

, comma, parallel time

For example, the * 6,12,18 * * command means that command is executed at 6, 12 and 18.

- minus sign, continuous interval

For example, * 9-17 * * command indicates that the command is executed every minute from 9 to 17.

/Slash, spacer unit

For example, */5 * * * command means that the command is executed every 5 minutes

System-Level Crontab

If we need to execute some higher-privileged instruction, we need to use root to execute, and the mechanism is different from the basic syntax described earlier, and the file we need to edit is/etc/crontab. Let's look at the contents.

dawang@dawang-parallels-virtual-platform:~$ cat/etc/crontab
#/etc/crontab:system-wide crontab
# Unlike any Other crontab you don ' t have to run the ' crontab '
# command to install the new version when you edit this file
# a nd files IN/ETC/CRON.D. These files are also have username fields, # that none of the other
crontabs do.

Shell=/bin/sh
path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m H Dom Mon Dow user Command * * * *
root CD/&& run-parts--report/etc/cron.hourly
6 * * * Root test-x/usr/sbin/ana Cron | | (CD/&& run-parts--report/etc/cron.daily)
6 * * 7 root test-x/usr/sbin/anacron | | (CD/&& run-parts--report/etc/cron.weekly)
6 1 * * Root test-x/usr/sbin/anacron | | (CD/&& run-parts--report/etc/cron.monthly)
#

We need to add the command performer between the command and the time interval, and we can also add environment variables to use in the schedule. We see a few cron.* files in the configuration file, let's see what else looks like.

dawang@dawang-parallels-virtual-platform:~$ Ll/etc | grep cron
-rw-r--r--1 root  401 December 2014 Anacrontab
drwxr-xr-x 2 root root 4096 April 06:14 cron.d/
   drwxr-xr-x 2 root 4096 April 06:14 cron.daily/
drwxr-xr-x 2 root root 4096 April 06:08 cron.hourly/drwxr-x
R-x 2 root 4096 April 06:14 cron.monthly/
-rw-r--r--1 root root  722 April 6 05:59 crontab
drwxr-xr-x 2 root Root 4096 April 06:14 cron.weekly/

which

cron.ddirectory: All files in the directory and subdirectories that match the scheduling syntax are executed

cron.deny: Record users who refused to execute

cron.allow: Records allowed to execute the user, this file has a higher priority, in general, only need to configure a file (see if you need a white list or blacklist mechanism)

cron.daily/hourly/monthly/weekly directory: Inside are all scripts, respectively, in the specified time to execute

For more detailed information, you can enter man 5 crontab or man 8 cron consult

Principle

Why we crontab -e can add a timed task by editing it? Each time we add a row, the job is recorded in/var/spool/cron/crontab, and if my username is Dawang, then the corresponding file is/var/spool/cron/crontab/dawang (requires root Permission to view). However, it is not recommended to modify directly, because the direct modification is not a grammar check.

In some systems, the source configuration file is not necessarily read every time (instead of using the version loaded into memory), we need to restart the Crond service at this time, the command is/sbin/service crond restart

Crond Service Management

The default system does not start the Crond service for us, if you want to boot up, you need to add the service Crond start in/etc/rc.d/rc.local, the other management commands are

# Start Service
/sbin/service crond start 
# Close service
/sbin/service crond Stop 
# Restart service
/sbin/service Crond Restart 
# reload configuration
/sbin/service Crond Reload

Instance test

And then we're going to fight, the first time crontab -e we need to select the editor, the default is the nano, but I chose vim

dawang@dawang-parallels-virtual-platform:~$ crontab-e
No crontab for dawang-using a empty one

Select an editor . To the change later, run ' select-editor '.
 1./bin/ed
 2/bin/nano  <----easiest
 3./usr/bin/vim.tiny

Choose 1-3 [2]:

To verify that it is really executing, we build two operations per minute, as follows (focus on the last two lines):

# Edit This file to introduce the tasks to is run by Cron.
# each task to run has to is defined through a single line # indicating with different when the task would be run # and what command to run for the task # ~ Define the time can be provide concrete values for # minute (M), hour (h),  Day of Month (DOM), month (Mon), # and day of Week (Dow) or use ' * ' in this fields (for "any"). # Notice that tasks would
Be started based on the Cron's system # Daemon ' s notion of time and timezones. # Output of the crontab jobs (including errors) is sent through # Email to the user the crontab file belongs to (unless
redirected). # For example, your can run a backup of all your user accounts # at 5 a.m every week with: # 0 5 * * 1 tar-zcf/var/back ups/home.tgz/home/# For more information to the manual pages of crontab (5) and Cron (8) # m H Dom Mon Dow command * * * * Date >>/home/dawang/date.txt * * * * * echo "time to go!" >>/home/dawang/time.txt

Two things are done here, one is the hourly time, the other is the output per minute, where the >> is used to append output, and more input and output methods are described in the next section. If you have not started the service, now use the service crond start start, and then wait for a while, you can see the output, specific reference to the following command, here is not to repeat:

dawang@dawang-parallels-virtual-platform:~$ LL | grep txt
-rw-rw-r--1 dawang dawang 1849 July 16:08 date.txt-rw-rw-r--
1 dawang dawang 516 July 16:08 time.txt< c4/>dawang@dawang-parallels-virtual-platform:~$ tail-n date.txt
July 26, 2016 Tuesday 16:01:01 CST
July 26, 2016 star Phase II 16:02:01 CST
July 26, 2016 Tuesday 16:03:01 CST
July 26, 2016 Tuesday 16:04:01 CST
July 26, 2016 Tuesday 16:05:01 cst
   2016 year July 26 Tuesday 16:06:01 CST
July 26, 2016 Tuesday 16:07:01 CST
July 26, 2016 Tuesday 16:08:01 CST
July 2016 26th Tuesday 16:09:01 CST
July 26, 2016 Tuesday 16:10:01 CST
dawang@dawang-parallels-virtual-platform:~$ tail-n. TXT time to 
go!
Time to Go!
Time to Go!
Time to Go!
Time to Go!
Time to Go!
Time to Go!
Time to Go!
Time to Go!
Time to Go!

REDIRECT Command

Here we give an example directly

Command > file redirect standard output to file command
>> files append standard output to file
Command 1 > file redirect standard output to file
command 2 > File redirects standard error to document command
2 >> file append standard output to file
command 2>&1 redirect command standard error to standard output
command > file 2>&1 redirects standard output with standard error to file
command >> file 2>&1 appends standard output with standard error to file
command < file Input command < file >file2 The command command to the file file as standard
input, file2 file as standard output
command < &-Turn off standard input

Summarize

The above is the entire content of this article, I hope for everyone's study or work can bring certain help.

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.