【问题标题】:Restart a process when cpu gets highcpu变高时重启进程
【发布时间】:2011-07-01 11:44:57
【问题描述】:

我有一个 cron 作业检查网络服务器(查看它是否处于活动状态),这很方便..

http://pastebin.com/raw.php?i=KW8crfzh

我想要类似的 CPU 使用率。我正在运行 java 后端,它偶尔会获得 70% 以上的 cpu。如果 cpu 负载过高,我正在使用 cron 脚本来自动杀死/重新启动 java,这怎么可能?

【问题讨论】:

  • 当它打电话给我并询问希拉里·克林顿的电话号码时,我通常可以告诉我的 CPU 很高。
  • @Maxpm,具有讽刺意味的是,我有一个 cron 工作,它会为我检查 tomcats jkstatus 并在它过高时给我发短信。

标签: bash cron


【解决方案1】:

您可以在批处理模式下使用top 并结合一些代码来解析其输出。例如:

top -p 1234 -n 1 -b

将输出进程 1234 状态的快照。

【讨论】:

    【解决方案2】:

    我使用这个脚本,它很酷

    #!/bin/bash
    # author = Jaysunn
    
    # Log
    LOGFILE=/var/log/load_kill_log
    
    # log the process causing the load at the time.
    PSFILE=/var/log/ps_log
    
    # Obtain the server load
    loadavg=`uptime |cut -d , -f 4|cut -d : -f 2`
    thisloadavg=`echo $loadavg|awk -F \. '{print $1}'`
    
    if [ "$thisloadavg" -ge "10" ]; then
    
    ps auxfww >> $PSFILE
    date >> $LOGFILE
    
    # Issue the command of choice.  This can be any shell command.
    ## Put the command which restarts ..
    
    fi
    

    授予可执行权限并将其添加到具有该脚本正确路径的 crontab。

    【讨论】:

      猜你喜欢
      • 2017-10-19
      • 2012-03-06
      • 1970-01-01
      • 2014-03-14
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多