How to use crontab (note that you should remember to restart the crond after modifying)

Source: Internet
Author: User

Today I made a backup script of the database, and by the way the system had to learn the settings of the timed execution script under Linux. Linux scheduled execution is mainly used in the crontab file to add a custom plan to execute, the setup is slightly more complicated than Windows (because there is no graphical interface), but it is not very complex, basically used once to remember, the key is to remember/var/spool/ Cron this directory. Here's a look at the specific usage:First look at the/etc/crontab file:$ cat/etc/crontabShell=/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
The first four rows are the environment variables that set the cron task to run. The value of the shell variable specifies the shell environment used by the system (the example is Bash shell), which defines the path to execute the command. The output of Cron is sent as an e-mail message to theuser name defined by the M ailto variable. If the MAILTO variable is defined as an empty string (mailto= ""), the e-mail message is not sent. The home variable can be used to set the base directory when executing a command or script.
Each line of the task in the file/etc/crontab is described in the following format:
minute hour day month DayOfWeek command   minute -integers from 0 to 59
hour -integers from 0 to 23
Day-an integer from 1 to 31 (must be a valid date for the specified month)
month -an integer from 1 to 12 (or a month such as the Jan or Feb abbreviation)
DayOfWeek -integers from 0 to 7, 0 or 7 to describe Sunday (or as represented by sun or mon shorthand)
Command-commands to execute (commands that can be used as Ls/proc >>/tmp/proc or execute custom scripts)
Root indicates to run as root user
Run-parts means a folder followed by all the scripts under that folder   For each of these statements, the asterisk (*) represents all available values. For example, when referring to month, the command is executed monthly (subject to other restrictions ).
The hyphen (-) between integers denotes an integer sequence , for example 1-4 means integers 1,2,3,4
The specified value is separated by commas. Such as: 3,4,6,8 represents these four specified integers.
the symbol "/" specifies the stepping setting. "/<interger>" indicates a stepping value. such as the 0-59/2 definition is executed every two minutes. The step value can also be represented by an asterisk. such as */3 is used to run a specified task every three months running.  A comment line that begins with "#" is not executed.  If a cron task needs to be performed on a regular basis instead of by the hour, day, week, and month, you need to add the/ETC/CRON.D directory. All files and files in this directory are/etc/crontab in the same syntax, see the sample:  # Record the memory usage of the system every Monday
# at 3:30AM in the File/tmp/meminfo
3 * Mon cat/proc/meminfo >>/tmp/meminfo
# Run Custom Scrīpt the first day of every month at 4:10AM
4 1 * */root/scrīpts/backup.sh
users other than the root user can perform crontab configuration scheduled tasks. All user-defined crontab are stored under directory/var/spool/cron, and the task is executed as the creator. to create a crontab with a specific user, first log in as that user, execute command crontab-e, and the system initiates the editing crontab that is specified in visual or editor. The file content is the same as the/etc/crontab format. Examples are as follows:   0 3 * * */home/dbbackup/db1backup.sh Backup
0 4 * * */home/dbbackup/db2backup.sh backup  represents 3 points per day to perform/home/dbbackup/db1backup.sh backup,4 point execution/home/dbbackup/db2backup.sh Backup, if it is executed every five minutes can be changed to: */5 * * * * /home/dbbackup/db2backup.sh backup when the changed crontab needs to be saved, the file is saved in the file/var/spool/cron/username as follows. The file name differs depending on the user name.  The Cron service checks for changes in/etc/crontab,/etc/cron.d/,/var/spool/cron files every minute. If a change is found, it is downloaded to the memory. Therefore, even if the crontab file changes, the program does not need to be restarted. The recommended custom task is added using the CRONTAB-E command, which restarts the crond process with the/etc/init.d/crond Restart command , and the official file says that it does not restart the process, but I am not able to run the task without restarting. Start do not know/etc/crontab file run-parts what meaning, direct command in accordance with the/etc/crontab format plus always can not run, later only know run-parts refers to the folder followed by.

How to use crontab (note that you should remember to restart the crond after modifying)

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.