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

你可能感兴趣的:(Linux)