前言

某天在 YouTube 上打开小视频的时候,居然提示有广告屏蔽插件,要么停用屏蔽显示广告,要么买高级会员。

咱就是纯粹不想等待广告读秒,也不想中途突然跳出广告,根本不舍得买高级会员,所以想到在路由中屏蔽,smartdns 的广告过滤还蛮有效。

搞技术的都知道,这种东西是按配置不定期更新的,就是个你来我往,为了省事,就建了个 shell 脚本添加定时任务,没想在 OpenWrt 系统里和普通的 Linux 不太一样。

现象

先看 shell 内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

logfile="/root/smartdns-update.log"
Ts=$(date -R)
echo "start work at $Ts" >$logfile

ls -l /etc/smartdns/smartdns-a*.conf >>$logfile
echo "<<<<<<befor------after>>>>>>" >>$logfile

antiad_src="https://anti-ad.net/anti-ad-for-smartdns.conf"
antiad_dst="smartdns-antiad.conf"
wget --no-check-certificate ${antiad_src} -O ${antiad_dst} && mv -f ${antiad_dst} /etc/smartdns/${antiad_dst}

adrules_src="https://adrules.top/smart-dns.conf"
adrules_dst="smartdns-adrules.conf"
wget --no-check-certificate ${adrules_src} -O ${adrules_dst} && mv -f ${adrules_dst} /etc/smartdns/${adrules_dst}

ls -l /etc/smartdns/smartdns-a*.conf >>$logfile

smartdns restart
Te=$(date -R)
echo "smartdns has restarted at $Te" >>$logfile

脚本功能很纯粹,只是为了下载线上最新的配置并应用,手动执行功能没问题,但添加到路由定时任务后功能却未实现,使用 logread -e cron 可见定时任务执行过了:

1
2
Tue Nov  7 11:13:00 2023 cron.err crond[9487]: USER root pid 17170 cmd /root/smartdns-adblock.sh
Tue Nov 7 11:14:00 2023 cron.err crond[9487]: user root: process already running: /root/smartdns-adblock.sh

解决方法

一开始以为是路径的问题,但改成了绝对路径也一样的结果。原来 OpenWrt 中定时任务需要指定运行环境,最终添加的定时任务如下:

1
0 6 * * * . /etc/profile; /root/smartdns-adblock.sh

添加 . /etc/profile; 这段内容后,定时任务成功触发相应功能。