Scheduled script execution in Linux

Source: Internet
Author: User

In Linux, the scheduled execution script in Linux is mainly executed by adding a custom plan to the crontab file, mainly by remembering the/var/spool/cron directory. Next let's take a look at the specific usage: first check the/etc/crontab file: $ cat/etc/crontabSHELL =/bin/bashPATH =/sbin:/bin:/usr/sbin: /usr/binMAILTO = rootHOME =/# run-parts01 *** root run-parts/etc/cron. hourly02 4 *** root run-parts/etc/cron. daily22 4 ** 0 root run-parts/etc/cron. weekly42 4 1 ** root run-parts/etc/cron. the first four rows of monthly are environment variables related to setting cron task running. The SHELL variable value specifies the SHELL environment used by the system (this example is bash shell). the PATH variable defines the PATH for executing commands. Cron output is sent to the username defined by the MAILTO variable by email. If the MAILTO variable is defined as a null string (MAILTO = ""), the email will not be sent. When executing commands or scripts, the HOME variable can be used to set the base directory. The description format of each task in the/etc/crontab file is as follows: minute hour day month dayofweek command minute-integer hour from 0 to 59-integer day from 0 to 23-integer from 1 to 31 (must be the valid date of the specified month) month-an integer from 1 to 12 (or a month abbreviated as Jan or Feb) dayofweek-an integer from 0 to 7, 0 or 7 is used to describe Sunday (or abbreviated as Sun or Mon) command-command to be executed (as ls/proc>/tmp/proc or command to execute custom scripts) root: run-parts as the root user. It means that a folder is followed by all the scripts in the folder. For the preceding statements, asterisk (*) indicates all available values. For example, * indicates that the command is executed every month (other restrictions must be met. The hyphen (-) between integers indicates the integer column. For example, 1-4 indicates the integer 1, 2, 3, and 4. The specified values are separated by commas. For example, 3, 4, 6, and 8 indicate the four specified integers. The "/" symbol specifies the step setting. "/<Interger>" indicates the step value. For example, 0-59/2 is defined to be executed every two minutes. The step value can also be represented by an asterisk. For example, */3 is used to run a specified task every three months. Comments starting with "#" are not executed. If a cron task needs to be executed on a regular basis instead of by hour, day, week, or month, add the/etc/cron. d directory. All files in this directory have the same syntax as the/etc/crontab file. See the following example: # record the memory usage of the system every Monday # at 3: 30 AM in the file/tmp/meminfo 30 3 ** mon cat/proc/meminfo>/tmp/meminfo # run custom scr limit pt the first day of every month at AM 10 4 1 **/root/scr limit pts/backup. in addition to the root user, sh can execute crontab configuration scheduler tasks. All user-defined crontabs are stored in the/var/spool/cron directory, and tasks are executed as creators. To create a crontab for a specific user, log on to the user and run the crontab-e command. The system starts editing crontab in the editing software specified in VISUAL or EDITOR. The file content is in the same format as/etc/crontab. Example: 0 3 ***/home/dbbackup/db1backup. sh backup0 4 ***/home/dbbackup/db2backup. sh backup indicates that/home/dbbackup/db1backup is executed at three o'clock every day. sh backup, run/home/dbbackup/db2backup at 4 o'clock. sh backup. If it is executed every five minutes, it can be changed to: */5 *****/home/dbbackup/db2backup. sh backup when the changed crontab needs to be saved, the file will be saved as the following file/var/spool/cron/username. The file name varies with the user name. The cron Service Checks changes in the/etc/crontab,/etc/cron. d/,/var/spool/cron files every minute. If any change is found, it will be downloaded to the memory. Therefore, the program does not need to be restarted even if the crontab file changes. We recommend that you use the crontab-e command to add custom tasks. After exiting, use/etc/init. the d/crond restart command restarts the crond process. The official file says that you do not need to restart the process, but I cannot run the task without restarting it. At first, I don't know what the run-parts in the/etc/crontab file means. directly adding the command in the/etc/crontab format will always fail to run, later I learned that run-parts is followed by a folder.

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.