zoukankan      html  css  js  c++  java
  • 日志滚动与cron调度

    日志滚动与cron调度

    1、日志滚动

    nginx默认日志不能进行滚动,始终写入到一个文件中,即access.log。编写日志滚动的shell脚本,并使用linux的cron定时调度周期性进行日志文件滚动。

    1.1 编写滚动脚本

    1. 创建roll_log.sh脚本文件

      以root身份在/usr/local/bin/创建umeng_roll_log.sh文件

      #切换到root身份
      $>su root
      
      #进入指定目录,该目录是path搜索目录,任何位置都可以直接调用脚本
      $>cd /usr/local/bin
      
      #创建脚本文件
      $>nano umeng_roll_log.sh
      

      文件内容如下:

      #!/bin/bash
      dateStr=`date '+%Y-%m-%d-%H-%M'`
      mv /usr/local/openresty/nginx/logs/access.log /usr/local/openresty/nginx/logs/access.log.${dateStr}
      touch access.log
      openresty -s reload
      
    2. 修改权限,设置执行权

      $>su root
      $>chmod +x/usr/local/bin/umeng_roll_log.sh
      

    1.2 执行脚本,查看滚动效果

    $>sudo /user/local/bin/umeng_roll_log.sh
    

    2、使用linux crontab调度脚本

    2.1 编写调度脚本

    1. 切换到root账户,修改/etc/crontab调度文件

      $>su root
      $>nano /etc/crontab
      
    2. 输入如下内容内容如下:

      SHELL=/bin/bash
      PATH=/sbin:/bin:/usr/sbin:/usr/bin
      MAILTO=root
      
      # For details see man 4 crontabs
      
      # Example of job definition:
      # .---------------- minute (0 - 59)
      # |  .------------- hour (0 - 23)
      # |  |  .---------- day of month (1 - 31)
      # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
      # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
      # |  |  |  |  |
      # *  *  *  *  * user-name  command to be executed
      * * * * * root source /etc/profile;/usr/local/openresty/nginx/logs/roll_log.sh
      

    2.2 启动调度服务

    #启动linux调度服务(d表示daemon守护进程的意思,例如firewalld)
    $>sudo service crond start
    

    2.3 服务相关命令

    #服务控制
    $>sudo service crond stop		#停止服务
    $>sudo service crond restart	#重启crond
    $>sudo service crond status		#查看服务状态
    
    #开机自启
    $>sudo chkconfig crond on		#启用开机自启
    $>sudo chkconfig crond on		#禁用开机自启
    $>sudo chkconfig				#查看开启自启列表
    
  • 相关阅读:
    洛谷—— P3353 在你窗外闪耀的星星
    洛谷—— P1238 走迷宫
    洛谷—— P1262 间谍网络
    9.8——模拟赛
    洛谷—— P1189 SEARCH
    算法
    May 22nd 2017 Week 21st Monday
    May 21st 2017 Week 21st Sunday
    May 20th 2017 Week 20th Saturday
    May 19th 2017 Week 20th Friday
  • 原文地址:https://www.cnblogs.com/xupccc/p/9544650.html
Copyright © 2011-2022 走看看