一、shell脚本
[root@CentOs6 lampp]# vi test.sh #创建shell脚本
mypath='/tmp/logs' #日志备份到该目录下,定义变量使用单引号
mylog='/opt/lampp/logs/access_log' #我们要备份的日志
time=`date +%Y%m%d%H%M%S`#时间戳,执行命令使用``,esc下面的
cp ${mylog} ${mypath}/${time}_access.log #备份日志
echo "" > ${mylog}
[root@CentOs6 lampp]# ./test.sh #执行test.sh
-bash: ./test.sh: 权限不够 #会提示权限不够
[root@CentOs6 lampp]# chmod +x ./test.sh #需要给这它赋与权限
[root@CentOs6 lampp]# ./test.sh #再次执行,脚本没有报错
/tmp/logs
/opt/lampp/logs/access_log
20161227172323
/tmp/logs /tmp/logs/20161227172323_access.log
二、crontab 定时任务
[root@CentOs6 lampp]# crontab -e #编辑定时任务
* * * * * sh /opt/lampp/test.sh #每分钟执行一次test.sh,crontab使用在上篇Linux命令中详细介绍
[root@CentOs6 lampp]# crontab -l #查看定时任务
* * * * * sh /opt/lampp/test.sh
三、重启crond
[root@CentOs6 lampp]# service crond reload
重新载入 crond: [确定]
热门工具 换一换