Crontab is related to the user, each user has their own corresponding crontab.
Cron is a timed execution tool under Linux, and the following are commands for restarting/shutting down and so on
#/sbin/service crond start / start service #/sbin/service crond stop // shut down service #/ Sbin/service crond Restart // reboot service #/sbin/service crond reload // Reload Configuration #/sbin/service crond Status // view service statuses
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 Setting a user's Cron service, the root user needs this parameter when executing this command.
Crontab-l Listing details of a user's cron service
Crontab-r Delete a user's cron service
CRONTAB-E Editing a user's cron service
To set a timing parameter diagram:
crontab Special Symbol Description:
"*" represents all numbers within the range of values. Pay special attention to Oh!
"/" stands for every meaning, such as "*/5" means every 5 units
"-" represents a number to a number
"," scattered figures
For example:
30 21 * * * means 21:30 per night
45 4 1,10,22 * * = 4:45 per month for 1, 10, 22nd
now is the official code that compresses the logs for one months and deletes them .
Create a new. sh file under Logs's sibling directory for example: logzip.sh
Content:
Echo"Please wait ..."m= ' date-d"1 months ago"+%y-%m ' #获取上个月的yyyy-date string in mm format m2= ' date-d"1months ago"+%y%m ' Index=0F= ' Ls/home/hls/apache-tomcat-7.0.61/logs-1-C ' #获取logs下文件列表 (/home/hls/apache-tomcat-7.0.61/logs is the path where the log is located) forNameinch$f DoN= ' Expr"$name":'. *\ ([0-9]\{4\}-[0-9]\{2\}\). *'' #从文件名称中提取yyyy-mm Format Dateif["$n"!=""] && ["$n"="$m"] then f[$index]="/home/hls/apache-tomcat-7.0.61/logs/$name"#logs文件夹下符合要求的文件名称放入数组Elsef[$index]=""Fi (Index++)) Done Echo"$f"Str=${f[@]}if["${#str}"-gt0] #如果大于 0 then Zip/home/hls/apache-tomcat-7.0.61/logs/$m 2.zip $str #压缩数组中的文件为yyyymm. zip file, packaged and placed under logsElseEcho"No files found."Exit0fi echo"$m 2.zip maked, now delete the old files."RM-fr $str #删除已被打包文件 Echo"Done ."Exit0
How to use:
Perform crontab-e edit the current user timer task, go to the edit page, click O, enter 0 0 1 * */home/hls/apache-tomcat-7.0.61/logzip.sh
Press ESC to exit Edit and then: Wq Save to exit. This will set up a timed task in the crontab (the morning of the month to execute logzip.sh)
Note: The file carriage return characters created under Windows are not the same as the carriage return below Linux/unix, which may cause the script to fail to execute.
Use Crontab to pack logs and delete packaged logs under Linux