现在的位置: 首页 > 综合 > 正文

shell-001 撰写一个clearlog.sh脚本

2013年09月23日 ⁄ 综合 ⁄ 共 623字 ⁄ 字号 评论关闭

1. 撰写一个clearlog.sh脚本

[root@localhost shell]# cat clearlog.sh
#!/bin/bash
cp /dev/null /var/log/apache2/access.log

注:/dev/null大小为0,内容为空
拷贝空文件覆盖日志文件access.log,即清空该文件

2. 修改文件执行权限

chmod +x /root/shell/clearlog.sh

3. 

加入到crontab
crontab –u root -e
在vi中加入一行
0 6 * * * /root/shell/clearlog.sh
作用:每日早晨6点执行clearlog.sh脚本,即自动清理log文件

crontab 

循环执行例行性任务,有crond系统服务来控制

格式:
[分]      [时]       [日]       [月]      [周]                                    命令串
[0~59]  [0~23]  [1~31]  [1~12]  [0~7, 0和7都代表Sunday]
*/5        6-18    1,15      *           *    /etc/monitor.sh > /dev/null 2>&1

每隔5分钟,6至18时,每月初一、十五  执行后面的命令

用户的例行任务

crontab -u username –e

*/5        6-18    1,15      *     *    /etc/monitor.sh > /dev/null 2>&1

系统的例行任务

vi /etc/crontab 
添加一行,其中要注明user

抱歉!评论已关闭.