循环获取温度信息并上报到指定的路径,供其他程序读取
#!/bin/sh
##########################################################################################
function urlencode() {
if ! command -v curl >/dev/null 2>&1; then echo -E "$1"; return; fi
local str="$1"
curl -Gso /dev/null --data-urlencode "var=$str" "http://localhost/?" -w "%{url_effective}\n" | sed 's/^.*var=//'
}
function writeCache(){
CACHE_KEY="$1" ; CMD_URL="http://example.com/setx"
URL_DATA="k=${CACHE_KEY}&v=$(urlencode "$2")"
curl -L -m 5 -s -o /dev/null -d "$URL_DATA" "$CMD_URL"
}
function readCache(){
CACHE_KEY="$1" ; CMD_URL="http://example.com/get"
REQ_URL="$CMD_URL?key=${CACHE_KEY}"
curl -L --connect-timeout 3 -m 5 -s "$REQ_URL"
}
##########################################################################################
ROUTER_TEMP_KEY='example_key'
while true; do
cpu_temp=`cat /sys/class/thermal/thermal_zone0/temp`
writeCache "$ROUTER_TEMP_KEY" "$cpu_temp"
echo "$cpu_temp"
sleep 1
done
设置开机启动:/etc/rc.local
/root/gettemp.sh >/dev/null 2>&1 &