Cron under Linux is used to periodically perform set-up periodic instructions and is a built-in service for Linux, which can be started and shut down in the following ways:
// Start the service // Close Service // Restart Service // Reload Configuration
or use the/etc/init.d/cron [Start/stop/restart] Command
If the service starts automatically when the system starts, add it at the end of the/etc/rc.d/rc.local script:
/sbin/service Crond Start
- The Cron service provides the crontab command to set the Cron service, and here are some of the parameters and instructions for this command:
crontab-u//Set a user's Cron service, which is usually required by the root user when executing this commandcrontab-l//list details of a user cron servicecrontab-r//delete a cron service with no userscrontab-e//Edit a user's cron serviceinput: crontab-e, sometimes you may choose an editor, select it, enter edit mode and add tasks according to format
.----------------minutes (0-59)
|.-------------hours (0-23)
| |.----------Month Day (1-31)
| | |.-------month (1-12) or jan,feb,mar,apr ...
| | | |----Sunday (0-6) (sunday=0 or 7) or Sun,mon,tue,wed,thu,fri,sat
| | | | |
* * * * * [user name] [optional parameters] Command
The first 5 fields can also use several special symbols that are "*", "/" and "-", ",", * represent all the values within the range of numbers, "/" for each meaning, "*/5" means every 5 units, "-" represents a number to a number, "," to separate several discrete numbers
In the optional parameters this to mention,run-parts, if the command after using this parameter is the folder path, execute this path under all the scripts, such as
4 * * * Root run-parts/etc/cron.daily #每天4:02 Execute script within/etc/cron.daily
after editing the save exit, automatically generate a file with the same name as this user under/var/spool/cron, cron starts every minute to read all the files in/var/spool/cron, check whether to execute the command inside, Therefore, the Cron service does not need to be restarted after this file has been modified
- Edit the/etc/crontab file configuration Cron,cron service reads/etc/crontab once per minute, with crontab configuration for a user, this file for the system task, add the format of the task ibid.
The following is a list of cron related files
/etc//etc//var/spool/cron/ #是所有用户的crontab文件目录
/var/spool/cron/#任务有输出或出错邮件目录
here is a simple task that I set up to delete log files periodicallywrite command in/opt/app/hadoop/mylogs_clear.sh
find/opt/app/hadoop/zk*/logs/version-2/-mtime +20-type f-name "log.*"-exec rm-rf {} \; #删除修改日期20天前的以log. The beginning of the log
#find/opt/app/hadoop/zk*/logs/version-2/-mtime +20-type f-name "log.*"-exec mv {}/opt/app/hadoop/logs_bk/\;
Execution: chmod u+x/opt/app/hadoop/mylogs_clear.sh to elevate execution permissions, it is best to confirm that there is no running permission, otherwise there will be willing to cause the failure of the operation
Then, CRONTAB-E Edit Join task
9 * * 4/opt/app/hadoop/mylogs_clear.sh #周四9:40 execution
Restart the Cron service if you are not at ease
Reference article:
1. Linux Application Summary (1): Automatically delete n days ago Log
2. Common usage examples for Find in Linux
3. Use of CRONTAB commands under Ubuntu
Cron services and applications for Linux