shell实现让路由器按要求定时发BTC价至手机
有什么用
让路由器按要求定时发BTC价至手机
前提条件
API需要魔法上网来访问
Operwrt的软路由,或者自己刷了OP系统的路由器
下一步
感受(缺点):shell真不是人写的,语法太魔性;下一步修改用python实现吧
优点:由于中间没有经过任何解释器,再直接依托于系统级别的cron定时任务,贼稳定,执行优先级也高
实现方法
找到两个可用的API
- curl -x “http://127.0.0.1:7890” -s https://api-pub.bitfinex.com/v2/ticker/tBTCUSD | awk -F ‘,’ ‘{print $7}’
- curl -x “http://127.0.0.1:7890” -s http://api.coindesk.com/v1/bpi/currentprice.json | python -c “import json, sys; print(json.load(sys.stdin)[‘bpi’][‘USD’][‘rate’])”
发送方案一:微信推送
-
“`bash
!/bin/sh
priceText=$(curl -x “http://192.168.6.116:7890” -s https://api-pub.bitfinex.com/v2/ticker/tBTCUSD)
$priceText|awk -F ‘,’ ‘$7’
price=$(echo $priceText|awk -F ‘,’ ‘{print $7}’)
curl -X POST -H “Content-Type: application/json” -d ‘{
“title”: “【BTC】:’${price}’ “,
“content”: “‘${price}’\n—-\n##### 内容1\n 设备1\n 设备2\n—-\n##### 内容2\n1”,
“token”: “6275de3c34fd40ce86a3d7dc5d98465b”,
“template”: “markdown”
}’ “http://www.pushplus.plus/send”
“` -
/etc/init.d/cron start
-
定时设置
-
![]()
-
添加
0 */3 * * * /usr/share/wechatpush/btc.sh &
crontab -l //显示crontab文件
service cron status
上传btc.sh
打开 http://192.168.6.1:8080/cgi-bin/luci/admin/system/fileassistant
测试用的shell命令:
#!/bin/sh
priceText=$(curl -x "http://192.168.6.116:7890" -s https://api-pub.bitfinex.com/v2/ticker/tBTCUSD)
#$priceText|awk -F ',' '$7'
price=$(echo $priceText|awk -F ',' '{print $7}')
if [ ${price} -gt '39000' ]
then
curl -X POST -H "Content-Type: application/json" -d '{
"title": "【BTC】:'${price}' ",
"content": "'${price}'\n----\n##### 内容1\n 设备1\n 设备2\n----\n##### 内容2\n1",
"token": "6275de3c34fd40ce86a3d7dc5d98465b",
"template": "markdown"
}' "http://www.pushplus.plus/send"
fi
切换到这个目录(举个例子)/usr/share/wechatpush/
添加计划任务
上传文件后修改计划任务
-
打开http://192.168.6.1:8080/cgi-bin/luci/admin/system/crontab
-
修改内容为:
shell
*/45 * * * * /usr/share/wechatpush/btc.sh
这样每分钟都是会执行,用来测试立即执行的效果;当然也可以直接shell里面敲入./btc.sh执行
{%note warning%}
过了2天,发现这个添加到计划任务里面自定义的这行,居然消失了
原因待查明, to be continued…
{%endnote%}
logread -e cron
读取日志发现:
最后一次执行记录(昨晚7点)
Mon Dec 4 19:00:00 2023 cron.info crond[31979]: USER root pid 6769 cmd /usr/share/wechatpush/btc.sh
测试解决方法:
测试使用 '定时执行任务/开机启动任务设置'
https://@@@@@@/cgi-bin/luci/admin/control/autotimeset/base
这个脚本是会保存的,设置了每小时自动执行
第二天:
语法纠正
. /usr/share/wechatpush/btc.sh
设置纠正
*/1 * * * * /usr/bin/timesethandler customscript Scheduled_task
测试解决办法#2:
shell脚本如何写信息到Openwrt系统日志中
一些定时的脚本,有时不清楚它是否执行了,是否成功全部执行,是否按时执行;如何保证?还是写相应的log信息到系统日志中,方便审计和后期查看。
logger是一个shell命令接口,可以通过该接口使用Syslog的系统日志模块,还可以从命令行直接向系统日志文件写入一行信息。
语法:
logger [options] [messages]
-i 在每行都记录进程ID
-t logger_test 每行记录都加上“logger_test”这个标签
Reference:https://www.cnblogs.com/rohens-hbg/articles/9599126.html
Sample:
ping 192.168.0.1 | logger -it btc.sh -p local3.notice &
#输出ping的每个输出给日志
实例:logger -t btc.sh -p local3.notice ${price}
实际[日志输出](http://192.168.2.1/cgi-bin/luci/admin/status/logs/syslog):Sun Dec 24 16:21:53 2023 local3.notice btc.sh: 43669
单独执行已经可以在日志中输出想要的执行过程信息,那么
如何让一个脚本呼叫执行另外一个shell脚本?
exe /usr/share/wechatpush/btc.sh
或
sh /usr/share/wechatpush/btc.sh
可实际上是 /usr/bin/timesethandler 在执行整个脚本
那,/usr/bin/timesethandler 如何输出日志
放弃在Openwrt中执行,因为日志输出无法正常,无法校验。
放到NAS中去,群晖NAS
新建 控制面板 – 计划任务
如何查看群晖NAS日志? 日志中心;
即便手动 运行也任何日志输出! ssh中运行看看
也是无法看到想要的预期效果,可以正常,看到NAS生成测试每分钟的日志文件(空内容)。
切换工具到debian中
crontab -e
成功添加进每分钟执行的 命令:
*/1 * * * * /root/Documents/btc.sh
如何查询debian中的日志?
Debian中Shell定时执行查询
- 查看当前的定时任务命令:crontab -e
- 定时每15分钟执行: */15 * * * * /root/Documents/btc.sh
- vi 编辑维护Shell脚本:/root/Documents/btc.sh
- 其中包含了wx推送的token,这样可以把想要的内容推送到微信
灵感来源
说明文档很清晰:Cron 和 crontab
现实应用,勉强能用…
还是期待下一步的python实现
题外话:这个项目“假装在中国”是学习shell的好实际应用 https://github.com/gaocuo/fic

发表回复