Linux Crontab Scheduled Tasks

Source: Internet
Author: User
Tags virus scan

1. Role
Use the crontab command to modify the crontab configuration file, which is then configured by the Cron utility when appropriate

, and this command uses permissions that are all users.

2. Format

The code is as follows Copy Code

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

3. Main parameters

-E: Execute the text editor to set the time table, the default text editor is VI.
-R: Deletes the current schedule table.
-L: Lists the current schedule tables.

The format of the crontab file is "M H d M D cmd". where m stands for minutes (0~59), H stands for hours (

0~23), D stands for Day (1~31), M stands for month (1~12), D represents the day of the week (0~6,0 for star

Days). CMD represents the program to run, it is sent to sh execution, this shell only user, home, shell

Three environment variables.

4. Notes

The crontab command is suitable for tasks that complete a fixed cycle, compared to the AT command.

5. Application examples
To set a timed, periodic system prompt:
[Cao @www Cao] #crontab-E
The system opens a VI editor.
If you enter the following: 5 Wall "Tomorrow is Saturday I'll go CS",
So every Friday 17:35 system will pop up a terminal, remind Saturday can play CS!

Use the crontab command to implement a daily, timed virus scan
A simple crontab command operation has been introduced, and there are some more important operations to look at.

  code is as follows copy code

* * */opt/lampp/lampp restart
#上面的例子表示每晚的21:30 reboot Lampp.

4 1,10,22 * */opt/lampp/lampp restart
#上面的例子表示每月1, 10, 22nd 4:45 restart LAMPP.

1 * * 6,0/opt/lampp/lampp restart
#上面的例子表示每周六, Sunday 1:10 restart LAMPP.

0,30 18-23 * * */opt/lampp/lampp restart
#上面的例子表示在每天18: Restart LAMPP every 30 minutes between 00 and 23:00.

0 * * 6/opt/lampp/lampp restart
#上面的例子表示每星期六的11: PM reboot lampp.

* */1 * * */opt/lampp/lampp restart
#每一小时重启lampp

* 23-7/1 * */opt/lampp/lampp restart
#晚上11点到早上 7-point, reboot every hour lampp

0 4 * mon-wed/opt/lampp/lampp restart
#每月的4号与每周一到周三的11点重启lampp

0 4 1 hours */opt/la Mpp/lampp Restart
#一月一号的4点重启lampp

/sbin/service crond Start//boot service
/sbin/service Crond Stop//Off service
/sbin/service crond Restart//Restart service
/sbin/service Crond Reload//Reload Configuration

View, edit, and delete
Cron stores the command line in a crontab (cron table) file, which is usually in the/etc directory. Each system user can have its own crontab (under/var/spool/cron/). To view the current user's crontab, enter Crontab-l, edit the crontab, enter CRONTAB-E, delete crontab, and enter Crontab-r. If you are currently root, to view/edit/delete/crontab a user, simply add-u USERNAME (such as crontab-e-U USERNAME) after the corresponding command. The default editor for crontab files is VI, you can enter export visual= ' editor ' to change the default editor.

The Cron service will not only read all the files in the/var/spool/cron directory once per minute, but also read the/etc/crontab files once. Configuring this file also allows Cron to perform tasks. Using the crontab command is a configuration of user-level tasks, while editing/etc/crontab files is a configuration of system-level tasks.

Syntax description
The following is an example of two cron statements (in the/etc/crontab file). The former is used for nightly backup of the/etc directory, which runs the analog program to process server statistics.

3 * * * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>&152 5 * * * * root/usr/local/ Src/analog-5.32-lh/analog >>/dev/null 2>&1 The following are field and field descriptions in cron statements:

Field description
1 minutes (0-59)
2 hours (2-24)
3rd period (1-31)
April (1-12; or initials, Feb, etc.)
5 weeks (0-6,0 for Sunday; or word abbreviation sun, Mon, etc.)
6 username (as the user when executing the command)
7 commands to execute (PATH)

Now look at the first line:

3 * * * * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>&1 This statement will run T at 3:12 A.M. (03:12) every day AR czf/usr/local/backups/daily/etc.tar.gz/etc command. >>/dev/null 2>&1 indicates that all standard output is sent to the/dev/null (the Linux Recycle Bin) and the standard error output (2) is sent to the same place (i.e./dev/null) as the standard output (1). Running this line of command will not produce any output.

This statement can be a little more complicated:

6 1 * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>&1 It will run tar in Monday of June 13 15:30 CZF/USR/LOCAL/BACKUPS/DAILY/ETC.TAR.GZ/ETC command.

The following statements can achieve the same effect:

June Mon * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>&1 If you want to be the user Joey's identity every hour of the first 15 minutes to run a program, you can use:

* * * * * Joey/usr/bin/somecommand >>/dev/null 2>&1 the asterisk (*) is a wildcard character, indicating that Cron will ignore this field.

If you want to run a program every two hours, you can use */2 in the hour field. It will be at 2 O ', 4, 6 ... 22 o'clock, 24 o ' work. The specific statements are as follows:

0 */2 * * * * Joey/usr/bin/somecommand >>/dev/null 2>&1cron statements can also use commas (,) to specify multiple times. For example, if you want to run a program at 15 points and 30 minutes per hour, you can use 15, 30 in the minute field:

15,30 * * * * * Joey/usr/bin/somecommand >>/dev/null 2>&1 If you want to run a program at a specified time of the first week of the month (that is, 1th to 7th), you can use 1-7 in the Date field:

15,30 */2 1-7 * * Joey/usr/bin/somecommand >>/dev/null 2>&1 This statement will be 15 minutes and 30 minutes (第1-7 ...) for every two hours of the 02:15,02:30 day of the month. 22:15,22:30, etc.) to run the/usr/bin/somecommand command.

If you want to execute a collection of scripts at 16:18 a day, you can put all the scripts you want to execute into a directory (such as/home/username/cron), and you can use:

* * * * * root Run-parts/home/username/cron >>/dev/null 2>&1 If you want to save the output of a program, you can put >>/dev/null 2> &1 replaced by >>/home/user/somecommand.log 2>&1.

The crontab command is used to perform specified commands at timed intervals, similar to the "Scheduled Tasks" under Windows, and is often used to do repetitive work.

Users of the Linux system simply add the sequence of commands to the crontab file, and the operating system executes the sequence of commands at user-configured times.

Before you add instructions to the crontab file, you need to check to see if the Crontab service is started and starts automatically:

See Crontab Service Status: Service Crond status

Manually start the Crontab service: Service Crond start

To see if the Crontab service is set to boot, execute command: NTSYSV

Open the service configuration to see if [] is selected before the Crond service, or select the service if it is not selected.

Using commands: Crontab-l View the scheduled task schedule for the current user

Edit crontab File command: CRONTAB-E

You can also create a crontab configuration file without using the CRONTAB-E command, and any other text editor writes a sequence of commands, such as the file name "Abc.cron", and then uses the command: "Crontab Abc.cron" to add the command sequence to the crontab.

By default, the Crond service is available to all users of the system. If you need to restrict the use of the Crond service, you can use profiles/etc/cron.allow and/etc/cron.deny, words too literally, Cron.allow is the user list that allows you to use Crond services, whereas users in Cron.deny

The syntax format for the crontab command is as follows:

Format one: crontab [-u user] File
This command is used to add a new crontab file

-U If you use this option, the specified user's crontab file will be modified, otherwise the crontab file that executes the crontab command user is modified.

Format two: crontab [-u user] [-e |-l |-r]

-L Displays the current crontab on the standard output.
-R Deletes the current crontab file.
-E uses the System Editor to edit the current crontab file. When the edits are complete, the files are automatically installed. This is a convenient way to take effect immediately after modification.

Nonsense for a long time, here is the point. The command format in the crontab file is as follows:

Minute hour day-of-month month-of-year day-of-week commands

namely: "Time-sharing" order, a total of 6 paragraphs, the first 5 paragraphs are times, the last paragraph is you want to carry out regular instructions. Each paragraph is delimited by a space or tab.

Take a look at the first 5 paragraphs, the date and time values range:

1, min [00-59]
2, hour [00-23]
3, day [01-31]
4, Month [01-12]
5, week [0-6]; Note that the 0 here represents Sunday

OK, let's take a simple example and get up 6:30 A.M. every day:

* * * echo "Get up Now!" >>/tmp/test.txt

Add the above instruction to the crontab file, and the system adds a line to the/tmp/test.txt file every 6:30 A.M.

The special symbol "*" is used in the above instruction. The meaning of "any", as in "*" in the SQL query statement "SELECT * from Tab", is to represent all numbers in the range of the current paragraph, such as "*" on the 3rd paragraph [01-31], and "*" on the 4th paragraph [ 01-12] months.

Special symbols In addition to the "*" Number and "/", "-", ",":

/represents "every", as "/5" means every 5 units;
-Used to represent a range, such as "[1-10]" representing 1 to 10;
, used to represent several discrete numbers, such as "5,15,25"

such as command: [0-59]/5 * * */sbin/shutdown-h now

Every 11 o'clock in the evening, every 5 minutes to try to turn off the machine, help you get into the good habit of early to bed and early to rise, hehe. When the machine is turned off, if it tries to boot, it will turn off again in multiples of 5, exactly like the Shockwave virus in Windows 2000.

00,10,20,30,40,50 * * * * echo "minutes has passed!" >>/tmp/test.txt

00 points, 10 points, 20 points, 30 points, 40 points, 50 points per hour to file "/tmp/test.txt" Write a line and 10 minutes ago.

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.