1. 程式人生 > >linux curl每秒請求一次 成功後終止

linux curl每秒請求一次 成功後終止

#!/bin/bash
while [ true ]; do
/bin/sleep 2  #幾秒請求一次
rst=`curl -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'User-Agent: Linux; U; Android 8.0.0; MI 6 MIUI/8.6.21' -d "p1=v1&p2=v2" --compressed 'https://xxx.com/xxx/xxx'`

printf '%s %s\n' `date "+%Y-%m-%d %H:%M:%S"` $rst
done

[]注意空格,數值運算,雙引號”“轉義,-gt -o注意前面有-

#!/bin/bash

filename="/home/xxx/`date +%F`"
if [ -f $filename ]; then
  exit
fi
while [ true ]; do

#rst=`curl -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'User-Agent:
Linux; U; Android 8.0.0; MI 6 MIUI/8.6.21' -d "p1=v1&p2=v2" 'https://xxx.com/xxx'`
rst=ok #echo $step step=$((step + 1)) #echo $step printf '%s %s\n' `date "+%Y-%m-%d %H:%M:%S"` $rst >> /home/xxx/xxx.log if [ "$rst" = 'ok' -o "$rst" == "success"]; then touch $filename exit fi #單引號不轉義 if [ $step -gt 100 ]; then exit 2 fi sleep 1 done

crontab設定問題,建議均以root身份執行

* 14
-19 * 9 * username date >> /home/xxx/111.log #新建的111.log使用者/組為root/root,以username執行,寫不進去 #即使手動更改111.log的owner為username:username也不行?? #在對應的使用者下執行crontab -e,即以該使用者身份執行

設定定時任務

* * * * * echo $PATH >> /home/shixiao/path.log
#輸出 /usr/bin:/bin

shell每秒執行一次 https://blog.csdn.net/wuchaoyu/article/details/51481203
Linux一個簡單的每秒執行命令shell指令碼 http://blog.chinaunix.net/uid-25266990-id-3302112.html
linux在shell中執行命令並將結果賦值給變數 https://blog.csdn.net/xs1326962515/article/details/78942115

crontab定時任務不執行的原因
https://blog.csdn.net/u011734144/article/details/54576469

關於crontab自動執行shell指令碼出現的不能執行命令問題
https://blog.csdn.net/fsx2550553488/article/details/81020623

Shell語法錯誤—-變數與空格[: too many arguments
https://blog.csdn.net/qq_22520587/article/details/62455740