自动重启 OS X 10.10.3 上问题多多的 discoveryd

众所周知,OS X 10.10.3 问题多多,其中用来取代 mDNSResponderdiscoveryd 常常出现 CPU 占用过高,无法正常工作的情况。

下面有一段简单的 Shell 脚本用以循环检查 discoveryd 是否出现问题,并自动重启它。需要管理员权限启动,可选的第一个参数是限制的最高 CPU 使用率(默认 80)。

#!/bin/bash

# Copyright (c) 2015, James Swineson 
# 
# 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): 
# //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

Github Gist 地址:https://gist.github.com/Jamesits/f5fb11af7c1b5cdd292b

使用方法:

wget https://gist.githubusercontent.com/Jamesits/f5fb11af7c1b5cdd292b/raw/bcf649f14ab7d936dc41efb77325ee16bcb4a428/discoveryd_checker.sh
chmod +x discoveryd_checker.sh
sudo ./discoveryd_checker.sh

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据