Linux command under crontab command _linux

Source: Internet
Author: User

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

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

1. Command format

crontab [-u user] file
crontab [-u user] [l | r | e] [-i] [s]
crontab-n [hostname]
crontab-c

2. Command function

Maintain timed task files for individual users

3. Command options

-U user

To edit a user's cron, use this parameter only with ROOT to set up a cron service for another user. can also be used to specify crontab files to the user.

-L

Lists the crontab for the current user.

-R

Deletes the current user's crontab.

-E

Edit 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 crontab directly.

4. Examples

Example 1: Lists scheduled tasks for the current user

[22:30:17] [Dodmaster@mha3 ~]$ Crontab-l
0 1 * * 1 $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 task

[22:30:17] [Root@mha3 ~]# crontab-u martin-e

Example 3: Specify a timed task file for the user Martin

[22:30:17] [Root@mha3 ~]# crontab-u Martin Martincron

5. cron file Syntax

First, we open the/etc/crontab file and we'll see the following:

[20:40:30] [Dodmaster@mha3 etc]$ cat/etc/crontab 
shell=/bin/bash
path=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
home=/
# for details, man 4 crontabs
# Example of job definition:
#.----------------Minute (0-59) c8/># |.-------------Hour (0-23)
# | |.----------DAY of Month (1-31) # | | |
-------Month (1-12) OR J
an,feb,mar,apr ... # | | | | |.----DAY of Week (0-6) (sunday=0 or 7) or Sun,mon,tue,wed,thu,fri,sat
#
| | | | # * * * * * user-name command to be executed

The file is a timed schedule for the system to execute, where the first four lines 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 for the cron service runtime, and no longer repeat here. mailto indicates that the Cron run task output is sent to the specified user as an e-mail message, 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 timing task.

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

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

3) The comma (,) represents the split specified value. For example: 3,5-7,9 represents 3,5,6,7,9.

4) A forward slash (/) represents a stepping value. For example, the location of the minute is */5, which means that it is executed every five minutes.

All user-defined crontab scheduled tasks other than root are stored in the/var/spool/cron directory, edited by 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 accordingly.

6. Examples

Example 1: Perform a system raid-check every one o'clock in the morning of the week

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

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

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

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

[21:57:56] [Dodmaster@mha3 ~]$ Crontab-l
8-20/4 * * * $HOME/. dailyshell/hislogclear.sh
Instance 4: Execute once every five minutes (two ways)

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

Instance 5: Performed every weekday eight o'clock in the afternoon

0 * * 1-5 echo "Every workday 20:00 to do it" >> time.txt

Instance 6:30 seconds per minute, because the cron service is awakened every minute, so the sleep command is required to perform tasks that are accurate to seconds.

* * * * * * * * * every minute second to do it >> time.txt
Instance 7: Executes every 20 seconds, which means that each time in 0 seconds, 20 seconds, 40 seconds per minute (or another value of 20 seconds, for example: 5s, 25s, 45s)

* * * * * echo "every second to do it" >> time.txt
* * * * * * * * * * * Every second to do it >> time.txt
* * * * * * * * * * Every second to do it >> time.txt

The above is a small set to introduce the Linux command under the crontab command, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.