linux 定时任务 cron
常用命令:
crontab参数
-u:这个参数可以让我们去编辑其他人的crontab,如果没有加上这个参数的话就会开启自己的crontab
crontab -u 使用者名称
-l:可以列出crontab的内容
-r:可以移除crontab
-e:可以使用系统预设的编辑器,开启crontab
-i:可以移除crontab,会跳出系统信息让你再次确定是否移除crontab
格式:
* * * * * /bin/bash ~/shelltest/mv_and_touch.sh
格式:
顺序 | 秒 | 分 | 时 | 日期 | 月份 | 星期 | 年(可选) |
取值 | 0-59 | 0-59 | 0-23 | 1-30(31) | 1-12 | 1-7 | 1970~2099 |
允许特殊字符 | , - * / | , - * / | , - * / | , - * / ? L W C | , - * / | , - *? / L C # | , - * / |
cron 命令:
/etc/init.d/cron start # 启动 或者 service cron start
/etc/init.d/cron stop # 停止 service cron stop
/etc/init.d/cron restart # 重启 service cron restart
/etc/init.d/cron reload # 重新加载 写完定时任务后可以使用
cron的日志
/var/log/cron.log
如果该文件下没有的话:
1. 修改rsyslog文件::
vi /etc/rsyslog.d/50-default.conf
将 rsyslog 文件中的 #cron.* 前的 # 删掉;
2. 重启rsyslog
service rsyslog restart
3. 重启cron
service cron restart
参考:
1. https://blog.csdn.net/ningningjj/article/details/104532157/
2.https://www.cnblogs.com/dongye95/p/10108696.html
贴上我测试的,日志按日期分割代码:
mv_and_touch.sh
#!/bin/sh
cd ~/shelltest
if [ ! -d date_log ]; then
mkdir date_log
fi
if [ ! -s 'access_nginx.log' ]; then
mv access_nginx.log ~/shelltest/date_log/access_$(date +"%Y-%m-%d_%H:%M:%S") # 时间一般设为 (date -I) 年-月-日, 这里只是为了看效果加上了 时分秒
touch access_nginx.log
else:
echo 'empty'
fi