In linux, crontab files are often maintained using the crontab command to submit scheduled jobs. Before introducing the crontab command, let's take a look at the format and meaning of each field in each entry in the crontab file. Each crontab entry has six fields, corresponding to 6... in the linux series, the crontab command is often used to maintain the crontab file, so as to submit a scheduled job. Before introducing the crontab command, let's take a look at the format and meaning of each field in each entry in the crontab file.
Each entry of crontab has six fields, corresponding to six columns, with 1st columns and 2nd columns from left to right. The following is a detailed introduction of these domains:
1st columns: Minute 1 ~ 59
Column 2nd: Hour 1 ~ 23
Column 3rd: Date 1 ~ 31
Column 4th: month 1 ~ 12
5th columns: 0 ~ 6 (0 indicates Sunday)
Column 6th: the command to run
In the first five columns, you can use the minus sign "-" to indicate the range, comma "," to represent or, and asterisk "*" to indicate the continuous time, if you do not have any special restrictions on a domain, you must enter the asterisk (*) in the domain "*". "*/5" indicates [hours, days, etc.] every five minutes. Each column must be separated by a space. See the following column:
58 12 ***/apps/bin/task. sh
Run the script task. sh at every day.
59 23, 20 **/apps/bin/backup. sh
Run the script backup. sh at on May 10 and May 20 every month.
30 12 ** 6, 0/bin/find-name "log"-exec rm {}\;
Execute commands at every Saturday and Sunday.
0, 30 18-23 ***/apps/bin/check. sh
Execute the script check. sh every 30 minutes from to every day.
*/3 */apps/bin/mysqlmonitor>/tmp/mysqlmonitor. log
Script execution per minute
After understanding the meaning of the crontab file format, we will start to introduce how to use crontab.
1. create a crontab file
A: create a job content file.
[Root @ localhost ~] $ Cat> mycron
*/2 */bin/echo 'date'>/dev/console
Enter the above content, and press ctrl + d to save the file mycron.
B: submit the job file mycron.
Crontab mycron
2. list crontab file content
Crontab-l
3. delete the crontab file
Crontab-r
3. edit the crontab file
Crontab-e
How to back up the crontab file. The crontab file of each user is saved in/var/spool/cron/by default/ File. If your username devw is added, your crontab file is/var/spool/cron/devw. you can back up the file directly. of course, you must have sufficient permissions.
If you want to run crontab, you must first install crontab to enable the crond service. The following describes how to install crontab and manage the crond service:
Note: The vixie-cron package is the main program of cron, and the crontabs package is used to install, uninstall, or list the tables used to drive the cron daemon process.
View crond service status
Service crond status
Start the crond service
Service crond start
Disable the crond service
Service crond stop
Restart the crond service
Service crond restart
Reload configuration with crond
Service crond reload
Add crond to start automatically
Chkconfig -- level 35 crond on
Check whether crond is enabled. run the following command: ntsysv.
Of course, for non-root users, after installing the software, starting the service, and setting the crontab file, the scheduled task may not be started. why? The permission is missing.
Cron uses/etc/cron. allow and/etc/cron. deny to allow or prohibit users from owning their own crontab files. Root users can create, edit, or delete these files. The entries in these files are user login names with one name per line.
How do I define permissions through these two files? If the following condition is true, you cannot use the crontab command:
The cron. allow and cron. deny files do not exist (only root users are allowed ).
The cron. allow file exists, but the user's login name is not listed in it.
The cron. deny file exists, and the user's logon is listed in it.
Note: If the user names in cron. allow and cron. deny exist, the user has the permission to run. Cron. allow has a higher priority than cron. deny.
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