LInux crontab and commands

Source: Internet
Author: User
Tags crontab example mail account crontab syntax

The

Timed task (cron job) is used to schedule commands that need to be executed periodically. With it, you can configure certain commands or scripts to run periodically over a set period of time. Cron is one of the most useful tools in Linux or Unix-like systems. The Cron Service (daemon) runs in the background of the system and continuously checks the/etc/crontab file and the/etc/cron.*/directory. It will also check the/var/spool/cron/directory. The

crontab command

Crontab is a command for installing, uninstalling, or listing a scheduled task list. The cron configuration file is used to drive the cron (8) Daemon of the Vixie cron. Each user can have their own crontab files, although these files are located in the/var/spool/cron/crontabs directory, but that doesn't mean you can edit them directly. You need to edit or configure your own scheduled tasks via the crontab command.

Timing profile Types

Profiles fall into the following different types:
UNIX or Linux system-level crontab: This type is typically used by those who need root or similar permissions for system services and important tasks to use. The sixth field (described in the field below) is the user name that specifies which user identity this command executes. As a result, the crontab of the system can perform operations as any user.
User crontab: Users can use the crontab command to install their own scheduled tasks. The sixth field is a command that needs to be run, and all commands run as the user who created the crontab task.

Note: The Cron implementation of this question-and-answer form is written by Paul Vixie and is included in many Linux distributions and Unix-like systems, such as the popular version fourth BSD. Its syntax is compatible with the implementation of various crond.

So how do I install, create, or edit my own scheduled tasks?
Create crontab file

The crontab file is obtained through the crontab command. Now suppose you have a user named Foxy, and you need to create your own crontab file. You can start by using any text editor to create a new file, and then write to it the command you want to run and the time you want to perform it periodically.
Then save the disk and exit. Assume that the file is/tmp/test.cron. After that, use the crontab command to install the file, making it the user's crontab file. Type:
   crontab Test.cron
Such a crontab file is set up. You can go to the/var/spool/cron directory and see a more foxy file. This file is the required crontab file. Using the more command to view the contents of the file, you can find that the header has three lines of information:
#DO not EDIT This file-edit the master and reinstall.
# (Test.cron installed on Mon Feb 22 14:20:20 1999)
# (Cron version--$Id: crontab.c,v 2.13 1994/01/17 03:20:37 vivie Exp $)
It probably means:
#切勿编辑此文件-If you need to change, edit the source file and reinstall it.
#test. cron File installation time: 14:20:20 02/22/1999
If you need to change the contents of the command, you still need to re-edit the original file, and then use the crontab command to install.
There is a limit to the users who can use the crontab command. If/etc/ Cron.allow file exists, only the user listed in it can use the command, and if the file does not exist but the Cron.deny file exists, only users who are not listed in the file will be able to use the crontab command, and if none of the two files exist, it depends on the settings of some parameters and may be allowed only by Superuser The command can also be used by all users.

The syntax format for the crontab command is as follows:
crontab [-u user] File
crontab [-U user]{-l|-r|-e}
The first format installs a new crontab file, installs files referred to by file, and if the "-" symbol is used as the file name, it means using standard input as the source of the installation.
-U If you use this option, you specify which specific user's crontab file will be modified. If you do not specify this option, Crontab will default to the * author's crontab, which means that the crontab file of the user executing the crontab command will be modified. Note, however, that if you use the SU command and then use the crontab command, it is likely that there will be confusion. So if you're using the SU command, it's a good idea to use the-u option to specify which user's crontab file.

crontab command

The Cron service provides the crontab command to set the Cron service, and here are some of the parameters and instructions for this command:

Crontab-u//Set a user's Cron service, which is usually required by the root user when executing this command
CRONTAB-L//list details of a user cron service
Crontab-r//Delete a user's cron service
CRONTAB-E//Edit a user's cron service (press X to delete the selected character of the cursor, press I to insert at the selected location, DD to delete the current line, P to paste)
: wq! +enter Save exit

: q! +enter Do not save exit
For example, root to view your cron settings: Crontab-u root-l
Again, for example, Root wants to delete Fred's cron settings: Crontab-u fred-r
When editing the Cron service, the edited content has some formatting and conventions, input: Crontab-u root-e
Enter VI edit mode, the content of the edits must conform to the following format: */1 * * * * ls >>/tmp/ls.txt
To edit your crontab file, you need to type the following command at the shell prompt for Linux or Unix:
$ crontab-e
crontab Syntax (field description)

The syntax is:
1 2 3 4 5/path/to/command arg1 arg2

Or
1 2 3 4 5/root/ntp_sync.sh

which
1th field: minutes (0-59)
2nd field: Hours (0-23)
3rd field: Date (0-31)
4th field: Month (0-12 [12 for December])
5th field: One day of the week (0-7 [7 or 0 for Sunday])
/path/to/command– the name of the script or command you plan to execute

Memory-friendly format:
* * * * * * command to be executed
----------------
| | | | |
| | | | Day of the----Week (0-7) (Sunday 0 or 7)
| | | ------Month (1-12)
| | --------One day in January (1-31)
| ----------hours (0-23)
------------minutes (0-59)

A simple crontab example:
# # # Run once every 5 minutes backupscript Script # #
*/5 * * * */root/backupscript.sh

# # # daily 1 o'clock in the morning run Backupscript script # #
0 1 * * */root/backupscript.sh

# # # of the first 3:15 run the Backupscript script # #
3 1 * */root/backupscript.sh

How to use Operators

operator allows you to specify multiple values for a field, there are three operators to use:
Asterisk (*): This operator specifies all available values for the field. For example, in the hour field, an asterisk is equal to every hour, and in the Month field, an asterisk is equivalent to a month.
Comma (,): This operator specifies a list that contains multiple values, for example: 1,5,10,15,20,25.
Bar (-): This operator specifies a range of values, for example: 5-15, equivalent to 5,6,7,8,9,..., 13,14,15 typed with the comma operator.
Delimiter (/): This operator specifies a stepping value, for example: 0-23/Can be used in the hour field to specify that a command is executed once per hour. The step value can also follow the asterisk operator, and you can use */2 if you want the command line to be executed every 2 hours.

How to disable message output

By default, the output of a command or script, if any, is sent to your local mailbox account. To stop receiving messages sent by crontab, you need to add >/dev/null 2>&1 to the back of the command you are executing, for example:
0 3 * * */root/backup.sh >/dev/null 2>&1

If you want to send the output to a specific mail account, such as [email protected] This mailbox, you need to define a MAILTO variable as follows:
mailto= "[Email protected]"
0 3 * * */root/backup.sh >/dev/null 2>&1

Visit the "Disable mailtips for Crontab command" to see more information.

Task: List all of your scheduled tasks

Type the following command:
# crontab-l
# crontab-u Username-l

To delete all scheduled tasks, you can use the following command:
# # # Delete current Scheduled Tasks
Crontab-r
# # # Delete a timed task under a user's name, this command needs to be executed as root user
Crontab-r-u username

Use special strings to save time
You can use one of the following 8 special strings to replace the first five fields, which will not only save you time, but also improve readability.
Special characters
Meaning
@reboot Run once every time you start
@yearly Run once a year, equivalent to "0 0 1 1 *".
@annually (same @yearly)
@monthly Run once a month, equivalent to "0 0 1 * *".
@weekly Run once a week, equivalent to "0 0 * 0".
@daily Run once a day, equivalent to "0 0 * * *".
@midnight (same @daily)
@hourly run hourly, equivalent to "0 * * * *".

Example:

Run the ntpdate command once per hour
@hourly/path/to/ntpdate

More about the/etc/crontab file and the/etc/cron.d/* directory

/etc/crontab is the system's crontab file. Typically only the root user or daemon is used to configure system-level tasks. Each individual user must use the crontab command to install and edit their own tasks as described above. The/var/spool/cron/or/var/cron/tabs/directory holds the crontab file of the individual user, which should be backed up in the user's home directory.

understand the default/etc/crontab file:
1, the first part of this format is the setting of the time, the latter part is to execute the command, if you want to execute too many commands, you can write these commands into a script, and then call this script directly here, the call to write the full path of the command. Time setting we have a certain agreement, the preceding five * number represents five numbers, the value range and meaning of the numbers are as follows:


Minutes (0-59)
Hours (0-23)
Date (1-31)
Month (1-12)
Week (0-6)//0 on behalf of Sunday

In addition to the numbers there are several special symbols are "*", "/" and "-", ",", * represents all the values within the range of the number, "/" for each meaning, "*/5" means every 5 units, "-" represents from a number to a number, "," separate several discrete numbers. Here are a few examples to illustrate the problem:

After editing a user's cron settings, Cron automatically generates a file with the same name as the user under/var/spool/cron, and the cron information for this user is recorded in this file, which cannot be edited directly and can only be edited with CRONTAB-E. After Cron starts, read the file once every one of the clocks, and check to see if you want to execute the command inside. Therefore, the Cron service does not need to be restarted after this file has been modified.


2, the typical/etc/crontab file content is this:
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root
home=/
# Run-parts
* * * * * Root run-parts/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily
4 * * 0 root run-parts/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly

First, the environment variables must be defined. If the shell line is ignored, Cron uses the default SH SHELL. If the path variable is ignored, there is no default search path, and all files need to be located using an absolute path. If the home variable is ignored, Cron replaces it with the home directory of the caller (user).

In addition, Cron reads the files in the/etc/cron.d/directory. Typically, a system daemon such as Sa-update or Sysstat will store their scheduled tasks here. As the root user or superuser, you can configure your scheduled tasks using the following directory. You can just put the script here. The Run-parts command runs a script or program that is located in a directory through the/etc/crontab file.


/etc/cron.d/places all the script files here and calls them from the/etc/crontab file.
/etc/cron.daily/run scripts that need to run once a day
/etc/cron.hourly/run a script that needs to run every hour
/etc/cron.monthly/run a script that needs to run once a month
/etc/cron.weekly/run scripts that need to run once a week

Backup Scheduled Tasks
# crontab-l >/path/to/file

# crontab-u user-l >/path/to/file

Example:

* * * * * command
Time-sharing Weekly command


The 1th column represents minutes 1~59 per minute with * or */1
The 2nd column represents the hour 1~23 (0 means 0 points)
The 3rd column represents the date 1~31
The 4th column represents the month 1~12
5th Column Identification Number Week 0~6 (0 = Sunday)
6th List of commands to run

Some examples of crontab files:

* * * * */usr/local/etc/rc.d/lighttpd restart
The above example shows a 21:30 restart lighttpd per night.

4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example shows a 4:45 restart lighttpd per month for 1, 10, 22nd.

1 * * 6,0/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example shows a 1:10 restart lighttpd per Saturday and Sunday.

0,30 18-23 * * */usr/local/etc/rc.d/lighttpd restart
The example above shows that LIGHTTPD restarts every 30 minutes from 18:00 to 23:00 every day.

0 * * 6/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example indicates that the LIGHTTPD is restarted every Saturday.

* */1 * * * */usr/local/etc/rc.d/lighttpd restart
Restart LIGHTTPD every hour

* 23-7/1 * * * */usr/local/etc/rc.d/lighttpd restart
From 11 o'clock to 7 in the morning, restart lighttpd every hour.

0 4 * mon-wed/usr/local/etc/rc.d/lighttpd restart
4th per month and 11-point restart lighttpd from Monday to Wednesday

0 4 1 Jan */usr/local/etc/rc.d/lighttpd restart
January 1 4-point restart LIGHTTPD

LInux crontab and commands

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.