标签(空格分隔): linux
[toc]
如何查看自定义的 logrotate 任务
ssh dropa
sudo su 变身管理员
crontab -l 查看定时任务,得到如下列表:
# 切割日志
0 0 * * * /usr/sbin/logrotate -vf /share/xuetang/nodejs-deploy/log-logrotate >/dev/null 2>&1 #每天凌晨定时切割日志
# 每1分钟向aws发送服务器信息
*/1 * * * * /share/aws-scripts-mon/mon-put-instance-data.pl --mem-util --swap-util --disk-path=/ --disk-space-util --disk-space-avail --aws-credential-file=/share/aws-scripts-mon/awscreds.conf --from-cron
0 2 * * * /bin/sh /share/xuetang/monitor/clear.sh
可以看到自定义的 logrotate 的定时任务是每天 0 点执行 logrotate,配置文件为 /share/xuetang/nodejs-deploy/log-logrotate,并且将输出信息销毁, stdout stderr 都转到 /dev/null。
cat /share/xuetang/nodejs-deploy/log-logrotate 得到如下返回:
/var/lib/docker/volumes/logs/_data/*.log {
missingok
daily
rotate 4
notifempty
sharedscripts
copytruncate
postrotate
echo end end end end end end ;
endscript
}
就是将 volumes 下的各种 log 文件以天为单位轮转,最多留到 4。
如何查看系统默认的 logrotate 任务
因为 logrotate 只是轮转日志,所以要每日轮转,还是要每日执行。系统默认的定时任务一般放在 /etc/cron.daily 目录。
cat /etc/cron.daily/logrotate 得到如下返回:
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
/usr/sbin/logrotate 就是 logrotate 的 shell 脚本,/etc/logrotate.conf 是默认配置。
cat /etc/logrotate.conf 得到如下返回:
# see "man logrotate" for details
# rotate log files weekly
weekly
...
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}
...
# system-specific logs may be also be configured here.
总结
所以我们是自己利用 crontab -e 去编辑自己的定时任务,让每天 0 点 以 /share/xuetang/nodejs-deploy/log-logrotate 为配置去执行 logrotate。
如何定时每天执行 container 里面的js代码
# /Users/liujunyang/work/xuetang/openplatform/fixreplay.js
/**
* 启动定时任务执行本脚本
* 在机器上(nodea nodeb)
* sudo su
* crontab -e
* 添加如下行即可
*/
#每天凌晨2点定时执行某个js
0 2 * * * sudo docker exec rain-oplat bash -c "cd /rain && node fixreplay.js"
# 没分钟写一行 123 到 /rain/abc 文件
*/1 * * * * sudo docker exec rain-oplat bash -c "cd /rain && echo 123 >> abc"