shell脚本定时刷新微信token实例

 

cron命令说明:

crontab -l  :display current user's schdule jobs

crontab -e : edit current user's schdule jobs

用法:

====================文件目录========================

===================== wechat.txt =====================

=====================  main.sh  =======================

#! /bin/bash
wechat_handler(){
    code=$1
#    echo ${code}
#    cat wechat.txt | jq .
    wechat_name=$(cat wechat.txt | jq ".wechats[${code}].property.wechat_name")
    #echo wechat_name ${wechat_name}
    length0=$(expr length "${wechat_name}")
    if [[ ${length0} == 0 ]];
    then
        echo Invalid json, please check json in wechat.txt
        exit 0
    fi

    app_id=$(cat wechat.txt | jq ".wechats[${code}].property.app_id")
    #echo app_id ${app_id}
    
    app_secret=$(cat wechat.txt | jq ".wechats[${code}].property.app_secret")
    #echo app_secret ${app_secret}
    
    access_token=$(cat wechat.txt | jq ".wechats[${code}].property.access_token")
    #echo access_token ${access_token}
    
    expired_at=$(cat wechat.txt | jq ".wechats[${code}].property.expired_at")
    #echo expired_at ${expired_at}
    
    now_time=$(date +"%s")
    #echo now_time ${now_time}
    
    if [ ${expired_at} -gt ${now_time} ]
    then
        echo ===============================
        echo \{\"access_token\":"${access_token}"\,\"expired_at\":"${expired_at}"\}
        echo ===============================
    else
        echo access_token expired
        echo start to refresh access_token
        refresh_token ${app_id} ${app_secret}
    fi
}

# Refresh token
refresh_token(){
    str0=$1
    str00=${str0:1}
    app_id=${str00%\"*}
    #echo ${app_id}

    str1=$2    
    str11=${str1:1}
    app_secret=${str11%\"*}    
    #echo ${app_secret}

    #Network request
        url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${app_id}&secret=${app_secret}"
         #echo ${url}
    result=$(curl ${url})
    #echo result:${result}
        access_token=$(echo ${result}|jq '.access_token')
    refresh_time=$(date +"%s")
    #echo refresh_time: ${refresh_time}    
    expired_at=$(expr ${refresh_time} + 7200)
    #echo expired_at: ${expired_at}
    
    echo =========== new access token ========================
    echo \{\"access_token\":"${access_token}"\,\"expired_at\":"${expired_at}"\}
    echo =====================================================
    
    rewrite_file ${app_id} ${expired_at} ${access_token}
    replace_file
}

#Refile access_token to file
rewrite_file(){
    app_id=$1
    expired_at=$2
    access_token=${3}
    #echo run rewrite file function.....
    #echo ${app_id} 
    #echo ${expired_at}
    #echo ${access_token}
    
    touch new_file.txt    

    #Read line from file
    flag=0

    while read line 
    do 
       line_content=$(echo $line)
       #echo ${line_content}
          if [[ "${line_content}" =~ "${app_id}" ]];then
        #echo match success,line_content= ${line_content}    
        echo ${line_content} >> new_file.txt
        flag=1
        #echo flag=${flag}
        continue    
       fi

       if [[ ${flag} == 3 ]];
       then
        #echo flag ${flag} rewrite access_token.....
        #echo access_token ${line_content}
        echo "\"access_token\"": ${access_token} ,>> new_file.txt
        flag=$(expr ${flag} + 1)
        continue
       fi
        
       if [[ ${flag} == 4 ]];
       then
        #echo flag ${flag} rewrite expire_time.....
        #echo expired_at ${line_content}
        echo "\"expired_at\"": ${expired_at} >> new_file.txt
        flag=$(expr ${flag} + 1)
        continue
       fi

       if [[ ${flag} == 1 || ${flag} == 2 ]];
       then
           flag=$(expr ${flag} + 1)
       fi

           echo ${line_content} >> new_file.txt
    done < wechat.txt
}

replace_file(){
    now_time=`date --date='0 days ago' "+%Y-%m-%d_%H:%M:%S"`
    mv wechat.txt wechat_${now_time}.txt
    mv new_file.txt wechat.txt
}

wechat_handler 0
======================  main.sh end ========================

touch file1.txt 更新file1.txt的存取和修改时间

touch -c file1.txt 如果file1.txt不存在,不创建文件

touch -r ref_file file1.txt 更新file1.txt的时间戳和ref+file相同

touch -t 0811142234.50 file1.txt 设定文件的时间错为08年11月14日22点34分40秒

=========================================================

转载于:https://my.oschina.net/xiejunbo/blog/661459

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值