Usage of CRONTAB commands under Linux

Source: Internet
Author: User

Cron comes from the Greek word chronos (meaning "time"), which is the next program in the Linux system that automatically executes the specified task. For example, if you want to create a backup of some files or folders every night while you sleep, you can use cron to come from execution.

Service start-up and stop

The cron service is a built-in service for Linux, but it does not boot automatically. You can start and stop the service with the following command:

Linux Code
    1. /sbin/service Crond Start
    2. /sbin/service Crond Stop
    3. /sbin/service Crond Restart
    4. /sbin/service Crond Reload

The above 1-4 lines start, stop, restart the service, and reload the configuration, respectively.

To set cron to start automatically when booting, in the/etc/rc.d/rc.local script can be added /sbin/service crond start .

View, edit, and delete

Cron saves the command line in the crontab (cron table) file, which is usually in the/etc directory. Each system user can have their own crontab (under/var/spool/cron/).

    • File uses the contents of the file as a cron service configuration for a user, and the contents are copied to the Cron service's configuration file
    • -U sets a user's cron service configuration, which is typically required by the root user when executing this command
    • -l lists a user cron service configuration
    • -R Delete a user's cron service configuration
    • -e Edit a user's cron service configuration, when specified crontab does not exist, new, will use VI to open the configuration file for editing save after the exit, file syntax see after
    • Crontab–v show The last edit time (available on some operating systems only)

To view/edit/delete/crontab of a user, simply add (for example) the corresponding command, if this is the current root identity -u USERNAME crontab -e -u USERNAME .

The default editor for the crontab file is VI, which you can enter to export VISUAL=‘editor‘ change the default editor.

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

Syntax description

Each line represents an instruction, which represents a scheduled task, each line consists of six parts separated by a space, the first 5 represents the time of the scheduled task execution, and the remainder is the command to be executed by the timed task, in the following format:

Hour of the week order
Minute Hour Day Month DayOfWeek command

    • Minute the first few minutes of each hour to perform the task, 0-59
    • Hour the first few hours of the day to perform this task, 0-23
    • Day of the month to perform this task, 1-31
    • Month months of the year, the task is performed, 1-12
    • DayOfWeek the day of the week to perform the task, 0-6,0 said Sunday
    • Command Specifies the program to execute

The first five items above can be used with the following four special symbols:

    • * Indicates all values, such as the first bit using * indicates every minute
    • /denotes each, as the first bit uses */5 to denote every 5 minutes
    • -Represents a range of values, as the second bit uses 2-4 to indicate 2 to 4 points
    • That represents a discrete number of values, such as the 2nd bit using 6,8 to represent 6 points and 8 points
    • Specify "step": 8-14/2 for 8,10,12,14
    • Specify a list: such as "1,2,3,4″," 0-4,8-12″

In the six fields of each row, except that the command is a field that must be specified each time, the other fields are optional fields, which can be determined visually. For a field that is not specified, "*" is used to fill its position.

1) Remove the first five paragraphs, starting from the sixth paragraph to the newline character or "%", for the command to be executed.

2) The default sixth paragraph will be executed by/bin/sh unless a non-/bin/sh shell variable is specified in the crontab file.

3) The "%" number in the sixth paragraph, unless preceded by the "\" number, otherwise, the "%" will end the command line, the meaning of newline, and then the content will be sent to the standard input to the preceding command.

Examples are as follows:

C code
    1. Every Minute (* * * * *)
    2. Every 5 minutes (*/5 * * * *)
    3. Twice an hour (0,30 * * * *)
    4. Once an hour (0 * * * *)
    5. Twice a day (0 0,12 * *)
    6. Once a day (0 0 * *)
    7. Once a week (0 0 * 0)
    8. 1st and 15th (0 0 1,15 *)
    9. Once a month (0 0 1 * *)
    10. Once a year (0 0 1 1 *)

Linux Code
  1. 5 * * * ls >/var/log.txt specify 5 minutes per hour to execute the LS command and save the result to the/var/log.txt file
  2. 5 * * * ls specifies 5 per day :30 execute ls command
  3. 7 8 * * ls designated 8th per month 7:30 minutes to execute the LS command
  4. 5 8 6 * ls specified every June 8th 5:30 execute the LS command
  5. 6 * * 0 ls specified every Sunday 6:30 executes the LS command [note:0 means Sunday,1 means Week 1, and so on , can also be expressed in English, Sun indicates that the star During the day, Mon says Monday and so on. ]  
  6. 3 * * ls 10th and 20th per month 3:30 execute the LS command [note: "," used to connect multiple discontinuous periods]
  7. 8-One* * * ls every day 8-11 points of 25 minutes to execute the LS command [note: "-" to connect a continuous period of time]
  8. * * the LS command is executed every 15 minutes [i.e. 0 60 minutes per hour for the LS command] /c11>
  9. 6 * *ls per month, every 10 days 6:30 The LS command is executed once [that is, 1, 11, 21,31st.Yes 6:< C11>30 executes the LS command once. ]  

Cron configuration file

Using the crontab command to add a completed scheduled task will generate a user name file in the/var/spool/cron directory, which is your scheduled task, and the Cron service will read the files in/var/spool/cron every minute.

System Scheduled Tasks

There is also a way to add a scheduled task, edit the/etc/crontab (cron service also reads the/etc/crontab file once per minute.)

The crontab configuration is for a user, while the edit/etc/crontab is a task for the system. The file format for this file is:

Linux Code
  1. Shell=/bin/bash
  2. Path=/sbin:/bin:/usr/sbin:/usr/bin
  3. Mailto=root//If an error occurs, or if there is data output, the data is sent to this account as an email
  4. home=///user-run path, here is the root directory
  5. # Run-parts
  6. #每小时执行 scripts within the/etc/cron.hourly
  7. * * * * * Root run-parts/etc/cron.hourly
  8. #每天执行 scripts within the/etc/cron.daily
  9. 4 * * * Root run-parts/etc/cron.daily
  10. #每星期执行 scripts within the/etc/cron.weekly
  11. 4 * * 0 root run-parts/etc/cron.weekly
  12. #每月去执行 scripts within the/etc/cron.monthly
  13. 4 1 * * Root run-parts/etc/cron.monthly

Note that the "run-parts" parameter, if removed, will be followed by a script name to be run instead of the folder name. Example: 2 * RM-RF/MNT/FB

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

Linux Code
    1. 3 * * * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>&1
    2. 5 * * * root/usr/local/src/analog-5.32-lh/analog >>/dev/null 2>&1

The following is a description of the fields and fields in the CRON statement:

Field Description
1 Minutes (0-59)
2 Hours (0-23)
3 Date (1-31)
4 month (1-12; or English abbreviation Jan, Feb, etc.)
5 Weeks (0-6,0 for Sunday; or word abbreviation sun, Mon, etc.)
6 User name (the user's identity when executing the command)
7 The command to execute (PATH)

Now look at the first line:

Linux Code
    1. 3 * * * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>&1

This statement will run the command at 3:12 A.M. (03:12) every day tar czf /usr/local/backups/daily/etc.tar.gz /etc . >> /dev/null 2>&1indicates that all standard output is sent to/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 become slightly more complex:

Linux Code
    1. 6 1 * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>& 1

It will run the command on the June 13 15:30 of Monday tar czf /usr/local/backups/daily/etc.tar.gz /etc .

The following statements can achieve the same effect:

Linux Code
    1. June Mon * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>&1 /c5>

If you want to run a program in the 15th minute of every hour as a user, Joey, you can use:

Linux Code
    1. * * * * * joey/usr/bin/somecommand >>/dev/null 2>&1

where the asterisk (*) is a wildcard character, which means Cron ignores this field.

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

Linux Code
    1. 0 */2 * * * Joey/usr/bin/somecommand >> /dev/null 2>&1

You can also use a comma (,) to specify multiple times in a cron statement. For example, if you want to run a program at 15 minutes and 30 minutes per hour , you can use it in the minute field 15,30 :

Linux Code
    1. * * * * *joey/usr/bin/somecommand >>/dev/null 2>&1

If you want to run a program for the first week of the month (that is, number 1th to 7th) , you can use it in the Date field 1-7 :

Linux Code
    1. 2 1-7 * * Joey/usr/bin/somecommand >>/dev/null 2>&1 /c3>

This statement will be on the第1-7 day of the month every two hours of 15 minutes and 30 minutes (02:15,02:30 ... 22:15,22:30, etc.) to run the /usr/bin/somecommand command.

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

Linux Code
    1. * * * * root run-parts/home/username/cron >>/dev/null 2>&1

If you want to save the output of a program, you can replace it with a >> /dev/null 2>&1 >> /home/user/somecommand.log 2>&1 .

Summarize
    • View the cron configuration for the current user, usingcrontab -l
    • Edit the cron configuration for the current user, usingcrontab -e
    • Delete the cron configuration for the current user, usingcrontab -r
    • To ask before deleting timer settings, use crontab-i interactive mode
    • View/edit/delete a user's cron configuration as root, and after the command, add-u USERNAME
    • Configure system-level tasks, edit/etc/crontab files

Ubuntu Reset crontab-e Default Editor

Java code
    1. sudo select-editor

On gnome-based Ubuntu Systems Gnome Scheduled Tasks tool (from the gnome-schedule package) inApplica tions -- System Tools provides a graphical interface with prompting for using Cron. The project website is at http://gnome-schedule.sourceforge.net/; The software is installable from the software Center or by typing

Java code
    1. sudo apt-get install Gnome-schedule

In a terminal.

Source: Https://help.ubuntu.com/community/CronHowto

Java code
    1. [Root@localhost ~]# crontab-l #查看定时运行的程序
    2. * * * * /bin/sh/var/www/database/bak.sh
    3. 3 * * */bin/sh/var/www/database/mylinux.sh
    4. 5 * * 0/sbin/reboot

Usage of CRONTAB commands under Linux

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.