In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Linux Crontab scheduled task command detailed explanation 2011-12-27 17:12 by Tracy., 23893 read, 0 comment, favorite, compile Linux Crontab scheduled task command detailed explanation
2011-12-27 17:12 by Tracy., 23893 Reading, 0 comments, favorites, Editing
The editor is loading.
one。 Crontab introduction
The function of the crontab command is to schedule the execution of some commands at regular intervals.
1.1 / etc/crontab Fil
There is a crontab file in the / etc directory, where there are some schedulers that the system is running. Each user can set up his own scheduling crontab.
Such as:
[root@dave ~] # cat / etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * root run-parts / etc/cron.hourly
02 4 * root run-parts / etc/cron.daily
22 4 * 0 root run-parts / etc/cron.weekly
42 4 1 * * root run-parts / etc/cron.monthly
1.2 / etc/cron.deny and / etc/cron.allow files
/ etc/cron.deny represents a user who cannot use the crontab command
/ etc/cron.allow represents a user who can use crontab.
If both files exist at the same time, / etc/cron.allow takes precedence.
If neither file exists, only the superuser can schedule the job.
Each user generates their own crontab file. These files are in the / var/spool/cron directory:
Such as:
[root@dave ~] # cd / var/spool/cron
[root@dave cron] # ls
Oracle root
Let's take a look at this file directly, and its content is consistent with the crontab-l displayed by the corresponding user.
[root@dave cron] # cat oracle
00 6 * / u02/scripts/del_st_archive.sh > / u02/scripts/del_st_arch.log 2 > & 1
[root@dave cron] # cat root
0 12 * / root/bin/sync-clock.sh
[root@dave cron] #
two。 Instructions for using Crontab
2.1 Crontab syntax
Usage: 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)
Where file is the name of the command file. If this file is specified on the command line, execute the crontab command and copy the file to the crontabs directory; if the file is not specified on the command line, the crontab command accepts commands typed on the standard input (keyboard) and stores them in the crontab directory as well.
Help:
[root@dave ~] # man crontab
CRONTAB (1) CRONTAB (1)
NAME
Crontab-maintain crontab files for individual users (ISC Cron V4.1)
SYNOPSIS
Crontab [- u user] file
Crontab [- u user] [- l |-r |-e] [- I] [- s]
DESCRIPTION
Crontab is the program used to install, deinstall or list the tables used to drive the cron (8) daemon in ISC Cron. Each user can have their own crontab, and though these are files in / var/spool/, they are not intended to be edited directly. For SELinux in mls mode can be even more crontabs-for each range. For more see selinux (8).
If the cron.allow file exists, then you must be listed therein in order to be allowed to use this command. If the cron.allow file does not exist but the cron.deny file does exist, then you must not be listed in the cron.deny file in order to use this command. If neither of these files exists, only the super user will be allowed to use this command.
OPTIONS
-u It specifies the name of the user whose crontab is to be tweaked. If this option is not given, crontab examines "your" crontab, I.E., the crontab of the person executing the command. Note that su (8) can confuse crontab and that if you are running inside of su (8) you should always use the-u option for safety??s sake. The first form of this command is used to install a new crontab from some named file or standard input if the pseudo-filename "-" is given.
-l The current crontab will be displayed on standard output.
-r The current crontab will be be removed.
-e This option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables. After you exit from the edi-tor, the modified crontab will be installed automatically.
-i This option modifies the-r option to prompt the user for a? Response before actually removing the crontab.
-s It will append the current SELinux security context string as an MLS_LEVEL setting to the crontab file before editing / replacement occurs-see the documentation of MLS_LEVEL in crontab (5).
SEE ALSO
Crontab (5), cron (8)
FILES
/ etc/cron.allow
/ etc/cron.deny
STANDARDS
The crontab command conforms to IEEE Std1003.2-1992 (POSIX????). This new command syntax differs from previous versions of Vixie Cron, as well as from the classic
SVR3 syntax.
DIAGNOSTICS
A fairly informative usage message appears if you run it with a bad command line.
AUTHOR
Paul Vixie
4th Berkeley Distribution 16 Januar 2007 CRONTAB (1)
2.2 Crontab format description
We can use crontab-e to add the command to execute. The results of the execution of the command, whether standard output or error output, will be sent to the user in the form of email.
The added command must be in the following format:
* / command path
The first five fields can be rounded to specify when to start working, and the sixth field is a string, or command field, which includes commands scheduled to be executed by crontab. The fields are separated by spaces and tabs.
The first five fields represent:
Minutes: 0-59
Hours: 1-23
Date: 1-31
Month: 1-12
Week: 0-6 (0 means Sunday)
You can also use some special symbols:
*: means at any time
,: indicates division
-: indicates a segment, such as 1-5 at the second end, which means 1-5 o'clock.
/ n: indicates that the unit of each n is executed once, such as in the second paragraph, * / 1, which means that the command is executed every 1 hour. It can also be written as 1-23 stroke 1.
Some examples:
00 8pr 12je 16 * / data/app/scripts/monitor/df.sh
30 2 * / data/app/scripts/hotbackup/hot_database_backup.sh
10 8 data/app/scripts/monitor/check_ind_unusable.sh 12 7 7 16 * /
10 8 data/app/scripts/monitor/check_maxfilesize.sh 12 7 7 16 * /
10 8 data/app/scripts/monitor/check_objectsize.sh 12 7 7 16 * /
43 21 * 21:43 execution
15 05 * 05:15 execution
0 17 * 17:00 execution
017 * * 1 every Monday at 17:00
0pr 10 17 * * 0je 2pm 3 executes at 17:00 and 17:10 every Sunday, Tuesday and Wednesday
0-10 17 1 * * from 17:00 to 7:10 on the 1st of January, at an interval of 1 minute
00 1pm 15 * 1 May 1st and 15th and 0:00 on the first day of the first month
42 4 1 * * at 4:42 on the 1st of June
021 * 1-6 from Monday to Saturday at 21:00
0, 10, 10, 20, 30, 40, 50 * every 10 minutes.
* / 10 * every 10 minutes
* 1 * every minute from 1:0 to 1:59
0 1 * 1:00 execution
Execute every hour at 0 * / 1 * 0pm
It is executed every hour at 0 * 0am.
2 8-20 beat 3 * 8 02ju 02ju 02ju 14purl 02ju 17ju 02ju 20purl 02 execute
Execution at 5:30 on the first and 15th of 3051 people
2.3 & execute commands in the background
When running a job in the foreground, the terminal is occupied by the job; when running the job in the background, it does not occupy the terminal. You can use the & command to put the job in the background.
Such as:
30 2 * / data/app/scripts/hotbackup/hot_database_backup.sh &
Be careful when running jobs in the background: commands that require user interaction should not be executed in the background, because your machine will be waiting there.
However, running the job in the background will also output the results to the screen, interfering with your work. If a job running in the background produces a large amount of output, it is best to redirect its output to a file using the following method:
Such as:
Command > out.file 2 > & 1 &
In this example, 2 > & 1 means that all standard output and error output will be redirected to a file called out.file.
2.4 2 > & 1 meaning
Let's first look at an example:
0 2 * / u01/test.sh > / dev/null 2 > & 1 &
This means to execute this command in the background and redirect error output 2 to standard output 1, and then put all standard output 1 in the / dev/null file, that is, empty.
There are several numbers here:
0 indicates keyboard input
1 represents standard output
2 indicates error output.
We can also write:
0 2 * / u01/test.sh > / u01/out.file &-- not written here, default is 1
0 2 * / u01/test.sh 1 > / u01/out.file &
0 2 * / u01/test.sh 2 > / u01/out.file &
0 2 * / u01/test.sh 2 > / u01/out.file 2 > & 1 &
Redirect the output of the tesh.sh command to out.file, that is, the output is not printed to the screen, but to an out.file file.
2 > & 1 redirects the error output to standard output. Then redirect the standard input to the file out.file.
& 1 represents file description 1 and standard output. If the missing & becomes the number 1, it means redirecting to file 1.
&: execution in the background
Test:
Ls 2 > 1: no error without 2 files will be reported, but an empty file 1 will be output
Ls xxx 2 > 1: no xxx file error output to 1
Ls xxx 2 > & 1: the file 1 will not be generated, but the error went to standard output
Ls xxx > out.txt 2 > & 1 = = ls xxx 1 > out.txt 2 > & 1; because the redirect symbol > defaults to 1, this sentence sends both error output and standard output to the out.txt file.
2.5 2 > & the reason why 1 is written later
Format: command > file 2 > & 1 = = command 1 > file 2 > & 1
First, command > file redirects the standard output to the file. 2 > & 1 is the standard error that copies the standard output, which is also redirected to the file. The end result is that both the standard output and the error are redirected to the file.
If you change it to: command 2 > & 1 > file
2 > & 1 standard error copies the behavior of standard output, but at this time the standard output is still on the terminal. The output is redirected to file after file, but the standard error remains on the terminal.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.