V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
zhjits
V2EX  ›  分享创造

分享一个自动重启 OS X 10.10 上 discoveryd 的脚本

  •  
  •   zhjits ·
    Jamesits · 2015-04-26 15:43:22 +08:00 · 2219 次点击
    这是一个创建于 3626 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最近 discoveryd 动不动 CPU 占用 100+,然后一堆应用都访问不了网络,十分难受。写了个脚本自动重启它。

    #!/bin/bash
    # Copyright (c) 2015, James Swineson <jamesswineson@gmail.com>
    #
    # Permission to use, copy, modify, and/or distribute this software for any
    # purpose with or without fee is hereby granted, provided that the above
    # copyright notice and this permission notice appear in all copies.
    #
    # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
    # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
    # AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
    # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
    # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
    # OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
    # PERFORMANCE OF THIS SOFTWARE.
    #
    # Gist URL: https://gist.github.com/Jamesits/f5fb11af7c1b5cdd292b
    # My blog post (in Chinese):
    # http://blog.swineson.me/restart-os-x-10-10-3-buggy-discoveryd-automatically/
    # Check if root
    if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root" 1>&2
    exit 1
    fi
    # Process max cpu usage arguments
    max_usage=80.0
    re='^[0-9]+([.][0-9]+)?$'
    if [[ $1 =~ $re ]] ; then
    echo "Max allowed CPU usage:" $1
    max_usage=$1
    fi
    # Check real CPU usage
    while true; do
    cpu_usage=$(ps -Aco command,pcpu | grep "discoveryd " | awk '{$1=""; sub(" ", ""); print}')
    if [ "${cpu_usage:-0}" \> $max_usage ] ; then
    echo "Warning: discoveryd CPU Usage at" $cpu_usage ", killing..."
    sudo killall discoveryd
    fi
    sleep 5
    done
    4 条回复    2015-04-26 19:11:59 +08:00
    Totoria
        1
    Totoria  
       2015-04-26 15:47:02 +08:00
    10.10.3正式版以后没遇到这个问题了。。
    zhjits
        2
    zhjits  
    OP
       2015-04-26 15:48:02 +08:00
    @Totoria 我这边正式版还是有……不知道为什么
    Csineneo
        3
    Csineneo  
       2015-04-26 18:48:28 +08:00
    sudo launchctl unload /System/Library/LaunchDaemons/com.apple.discoveryd.plist
    sudo launchctl load /System/Library/LaunchDaemons/com.apple.discoveryd.plist

    話說升級到 yosemite 之後只看到人們在說這個問題,還從來沒遇到過,不知道是不是和我用的是本地 dns server 有關
    zhjits
        4
    zhjits  
    OP
       2015-04-26 19:11:59 +08:00
    @Csineneo 这个跟我原理似乎一样。但是还是要不断监视它,过一段时间还是会出问题的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1150 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 17:37 · PVG 01:37 · LAX 10:37 · JFK 13:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.