为什么dnsmasq服务总是重启(by quqi99)

作者:张华 发表于:2020-05-19
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明

问题

最近, 是有点感觉到家里的网络有时有点慢, 特别有时候看网页时显示dns找不到,但马上在半秒之内又自动找着了成功显示页面. 能用也不算特别慢所以一直就没太注意这个事.
依平时的经验, 这种慢一般和dns有关, 所以今早就特地登录到路由器查看dnsmasq的状态. 检查了一遍配置啥的也没见异常, 但无意间发现dnsmasq的进程ID有时在变(不是很经常, 不容易观察出来).

dnsmasq: exiting on receipt of SIGTERM

然后就想着将 dnmasq进程停掉, 然后以debug模式(-d)启动看看日志, 但是当运行了/etc/init.d/dnsma stop之后怎么也停不掉啊, 一会儿进程又自动启动了. 读代码发现openwrt的init脚本使用了procd, 但将所有procd相关的行全注释掉之后, 停掉了但还是马上就自动启动了.
然后发现是/etc/rc.common在自动启动, 接着就在反复测试过程中形成了下列的代码.

root@OpenWrt:~# cat /etc/init.d/dnsmasq 
#!/bin/sh /etc/rc.common
START=80
start() {
      #/usr/sbin/dnsmasq --conf-dir=/tmp/dnsmasq.d --conf-file=/etc/dnsmasq.conf --user=dnsmasq --group=dnsmasq --server=114.114.114.114 -k &
   fi
}
stop() {
   killall dnsmasq
}

这时在前台以’/usr/sbin/dnsmasq --conf-dir=/tmp/dnsmasq.d --conf-file=/etc/dnsmasq.conf --user=dnsmasq --group=dnsmasq --server=114.114.114.114 -d’调试时发现dnsmasq进程自已会退出, 报这个错"dnsmasq: exiting on receipt of SIGTERM"
这时以为是dnsmasq程序有问题, 正在绝望之刻, 注意到了原因dnsmasq是被上面的stop杀死的. 通过/etc/init.d/dnsmasq stop根本停不住dnsmasq, 所以上面的stop函数中的killall dnsmasq在后台被定期运行, 这样就杀死了前台的debug dnsmasq进程.

解决办法

搞清楚’dnsmasq: exiting on receipt of SIGTERM’发生的原因之后, 将脚本暂时改成下列丑陋的代码:

COLUMNS=999 ps -ef |grep dns
oot@OpenWrt:~# cat /etc/init.d/dnsmasq 
#!/bin/sh /etc/rc.common
START=80
SERVICE_USE_PID=1
SERVICE_WRITE_PID=1
SERVICE_DAEMONIZE=1

start() {
# Only use 'if pidof dnsmasq >/dev/null' will see more and more processes when upstream network is down so change to use awk' NF
# and in some system it gives me 0 when binary not running and in some systems it gives me NULL(nothing)
if [ -z `pidof dnsmasq` ];then
    ulimit -c unlimited                                                                       
    echo "dnsmasq stopped, start it now ..."
    mkdir -p /var/lib/misc                                                                                                    
    service_start /usr/sbin/dnsmasq --conf-dir=/tmp/dnsmasq.ssr --conf-file=/etc/dnsmasq.conf --user=dnsmasq --group=dnsmasq \
      --server=221.130.33.60 --listen-address=192.168.99.1,127.0.0.1 --dhcp-range=192.168.99.100,192.168.99.199,3650d \
      --dhcp-option=3,192.168.99.1 --dhcp-option=option:dns-server,192.168.99.1 --cache-size=0
elif [ $(pidof dnsmasq |awk '{print NF}') -eq 1 ];then
    echo "dnsmasq is running"
else
    echo 'upstream network is down'
fi
}

stop() {
   #echo 'NOTE: we must not use 'killall dnsmasq' to stop dnsmasq here because stop function always be called by rc.common periodically'
   #killall dnsmasq >/dev/null
   #service_stop /usr/sbin/dnsmasq
   return
}

#don't custom restart so that it can be always invoked automatically to run start()
#restart() {
   #workaround to avoid killall dnamasq in stop() because do not know why restart is always invoked automatically
   #echo 'hint: both stop and restart not work for dnsmasq service'
   #exit 0
   #start
   #sleep 1
   #return
#}

server can’t find xxx.my.salesforce.com: REFUSED

另外, 解释白名单的dns没问题, 解析黑名单的dns也没问题, 但解析不在名单之内的dns时报’server can’t find xxx.my.salesforce.com: REFUSED’之类的错误时, 那是因为dnsmasq命令中没有添加–server=114.114.114.114

cache-size=0

–cache-size=0避免使用使用dnsmasq的缓存

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

quqi99

你的鼓励就是我创造的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值