How to write crontab in linux

Source: Internet
Author: User
Wscdlzhom was released on | 439 times font size: large, medium, and small (0 Comments by netizens) the crontab command function is used to schedule the execution of some commands at certain intervals. There is a crontab file in the/etc directory, which stores a file that has been running systematically...

Wscdlzhom published on | 439 times font size: large, medium, and small (0 Comments) I want to comment
The crontab command schedules the execution of some commands at certain intervals. There is a crontab file in the/etc directory, which stores some scheduling programs that are running systematically. Each user can create their own scheduling crontab.
 
The main configuration file of cron is/etc/crontab, which includes the following lines:
 
SHELL =/bin/bash
PATH =/sbin:/bin:/usr/sbin:/usr/bin
MAILTO = root
HOME =/
# Run-parts
01 *** root run-parts/etc/cron. hourly
02 4 *** root run-parts/etc/cron. daily
22 4 ** 0 root run-parts/etc/cron. weekly
42 4 1 ** root run-parts/etc/cron. monthly
 
The first four rows are variables used to configure the running environment of the cron task. The SHELL variable value tells the system which shell environment to use (bash shell in this example); the PATH variable defines the PATH used to execute commands. The cron task output is mailed to the username defined by the MAILTO variable. If the MAILTO variable is defined as a blank string (MAILTO = ""), the email will not be sent. The HOME variable can be used to set the HOME directory used for executing commands or scripts.
 
Each row in the/etc/crontab file represents a task. Its format is:
 
Minute hour day month dayofweek command
 
Minute-minute, any integer between 0 and 59
 
Hour-hour, any integer between 0 and 23
 
Day-Date, any integer between 1 and 31 (if a month is specified, it must be the valid date of the month)
 
Month-month, any integer from 1 to 12 (or use the abbreviated month as jan, feb, and so on)
 
Dayofweek-any integer between 0 and 7 in a week. here, 0 or 7 represents Sunday (or the abbreviation of a week, such as sun or mon)
 
Command-the command to be executed (the command can be ls/proc>/tmp/proc or a command to execute your own script .)
 
In any of the preceding values, the asterisk (*) can be used to represent all valid values. For example, the asterisk in the month value means that this command is executed every month after other constraints are met.
 
The short line (-) between Integers specifies an integer range. For example, 1-4 means integers 1, 2, 3, and 4.
 
Specifies a list of values separated by commas. For example, 3, 4, 6, and 8 indicate the four specified integers.
 
A forward slash (/) can be used to specify the interval. Adding/after the range means that integer can be skipped in the range. For example, 0-59/2 can be used to define every two minutes in the minute field. The interval value can also be used with the star number. For example, the value of */3 can be used in the month field to indicate that a task runs every three months.
 
The line starting with # is a comment and will not be processed.
 
As you can see in the/etc/crontab file, it uses the run-parts script to execute/etc/cron. hourly,/etc/cron. daily,/etc/cron. weekly and/etc/cron. scripts in the monthly directory, which are executed hourly, daily, weekly, or monthly accordingly. The files in these directories should be shell scripts.
 
A cron task can be added to the/etc/cron. d directory if it needs to be executed hourly, daily, weekly, or monthly. All files in this directory use the same syntax as those in/etc/crontab.
 
# 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 script the first day of every month at AM 10 4 1 **/root/scripts/backup. sh
 
You must add the permission and path when writing the. sh script executed by crontab. For example:
 
Crontab statement: 30 14 */opt/apps/EMS _query/bin/start. sh
 
Script:
Source/home/tvgame/. bash_profile
Cd/opt/apps/EMS _query/bin/
Java-Xmx64M-jar ../lib/EMS _query.jar &

Related Article

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.