Common Linux commands at work: crontab command

Source: Internet
Author: User

Crontab is a command used to set, delete, or display a timed task for the daemon cron to perform. Each user can have their own scheduled tasks, scheduled task files by default named after the user name, and placed in the/var/spool/cron directory, the directory of ordinary users have no access rights.

You can manage permissions for users using crontab through Cron.allow and Cron.deny files. If the cron.allow exists, the user must be listed in order to be allowed to use cron, and if the Cron.deny exists, the user being listed is prohibited from using cron, and if neither exists, only the superuser can use cron. In CentOS, these two files are placed in/etc, and only the Cron.deny file exists by default and is empty. This means that in CentOS, all users of the default have cron access.

1. Command format

crontab [-u user] File
crontab [-u user] [-l |-r |-e] [-I.] [-S]
CRONTAB-N [hostname]
Crontab-c

2. Command function

Maintain scheduled task files for individual users

3. Command options

-U user

Edit a user's cron, use this parameter only with ROOT to set up a cron service for other users. can also be used to assign a crontab file to a user.

-L

Lists the current user's crontab.

-R

Deletes the current user's crontab.

-E

Edits the current user's crontab, using VI by default, or an editor specified by the visual or editor environment variable.

-I.

Used with-R to ask the user whether to delete the crontab directly.

4. Example

Example 1: List the current user's scheduled tasks

[Max:crontab-l011 $HOME/. Dailyshell/hislogclear. SH 0 * * * * $HOME/. Dailyshell/mvcdr2bak. sh $HOME/data/message/Voice * * * * * $HOME/. Dailyshell/mvcdr2bak. sh $HOME/data/message/data/ * * * * * $HOME/. Dailyshell/mvcdr2bak. sh $HOME/data/message/sms/

Instance 2:root user Edit user Martin's timed tasks

[Max:crontab -u martin-e

Example 3: Specifying a timed task file for user Martin

[Max:crontab -u Martin Martincron

5. cron file Syntax

First, we open the/etc/crontab file, and we'll see something like this:

[ -: +: -][[email protected] etc]$Cat/etc/crontab SHELL=/bin/Bashpath=/sbin:/bin:/usr/sbin:/usr/Binmailto=Roothome=/# for details seeMans 4crontabs# Example of Job definition:#.----------------Minute (0- -)# | .-------------Hour (0- at)# |  | .----------Day of Month (1- to)# |  |  | .-------Month (1- A) OR jan,feb,mar,apr ... #|  |  |  | .----Day of Week (0-6) (sunday=0Or7) OR sun,mon,tue,wed,thu,fri,sat#|  |  |  | |# * * * * * * user-name command to be executed

This file is a timing plan for the execution of the system, where the first four rows are used to set the environment variables that the cron service runs: Shell, Path, and home specify the shell environment variables, PATH environment variables, and home environment variables that the cron service runs on, which are not discussed here. mailto indicates that the task output of the Cron run is sent as an e-mail message to the specified user, and if the value of the variable is empty, the message is not sent. The remaining lines describe the specific writing format of the system's scheduled tasks.

Minute Minutes, Value range 0-59
Hour Hours, Value range 0-23
Day of month Day, Value range 1-31
Month month, the value range of 1-12, or use the English abbreviation jan,feb,mar,apr ...
Day of week Week, the value range 0-6,0 or 7 represents Sunday, or the abbreviation is used Sun,mon,tue,wed,thu,fri,sat
User-name The user who performed the scheduled task
Command A specific command can be a simple command, a script, or a directory. If it is a folder, all scripts in that folder are executed, and run-parts must precede the file directory.

1) An asterisk (*) represents all values in the range of values. For example, * in hour position, it is performed once per hour.

2) a hyphen (-) indicates a range. For example, 8-12 means 8, 9, 10, 11, 12.

3) a comma (,) indicates that the specified value is split. For example: 3,5-7,9 represents 3,5,6,7,9.

4) The forward slash (/) indicates the step value. For example, the location of the minute is */5, which means that it executes every five minutes.

All user-defined crontab scheduled tasks except the root user are stored in the/var/spool/cron directory, edited with the crontab-e command, in the same format as/etc/crontab, without specifying user-name.

The Cron service checks all the files in/etc/crontab,/etc/cron.d/, and/var/spool/cron/every minute and executes them accordingly.

6. Example

Example 1: Raid-check of the system once every weekend one o'clock in the morning

0 1 * * Sun Root/usr/sbin/raid-check

Example 2:4:02am per day The root user executes all scripts in the/etc/cron.daily directory, and the Run-parts parameter represents execution of all scripts in the following directory.

 Geneva 4 * * * Root run-parts/etc/cron.daily

Example 3:8-18 every four hours per day, Dodmaster user executes hislogclear.sh script, which is 8:44,12:44,16:44,20:44 four point in time, executes the script.

[: £º] [Email protected] ~]$ crontab-l8-20/4* $HOME/. Dailyshell/hislogclear. SH

Example 4: Executes every five minutes (two ways)

*/5echo"every five minute to do it" time.txt5  echo"every five minute to do it" time.txt

Example 5: Every weekday eight o'clock in the afternoon execution

0 * "every workday 20:00 to do it time.txt   

Example 6: Executes at 30 seconds per minute, because the cron service is woken up once per minute, so a task that is accurate to the second requires the use of the Sleep command.

Sleep  - Echo " every minute second to do it " time. txt

Example 7: Every 20 seconds, that is, 0 seconds per minute, 20 seconds, 40 seconds each execution (also can be the other step is the value of 20 seconds, for example: 5s, 25s, 45s)

Echo " every second to do it "  Time . txt Sleep  - Echo " every second to do it "  Time . txt Sleep  + Echo " every second to do it " time. txt

Resources:

Http://www.cnblogs.com/dingyingsi/archive/2013/04/16/3023623.html

http://blog.csdn.net/xiyuan1999/article/details/8160998

Http://blog.chinaunix.net/uid-7552018-id-182133.html

Common Linux commands at work: 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.