代码之家  ›  专栏  ›  技术社区  ›  SaidbakR

将文件输出文本与crontab连接起来

  •  0
  • SaidbakR  · 技术社区  · 6 年前

    我已经成功地跟进了这个问题, Using CRON jobs to visit url? ,以维护以下cron任务:

    */30 * * * * wget -O - https://example.com/operation/lazy-actions?lt=SOME_ACCESS_TOKEN_HERE >/dev/null 2>&1
    

    上面的cron任务工作正常,每隔30分钟定期访问URL。

    但是,访问令牌记录在 /home/myaccount/www/site/aToken.txt ,的 aToken 文件是一个非常简单的文本文件,只有一行包含令牌字符串。

    我试着阅读它的内容并通过,使用 cat ,它指向crontab命令,如下所示:

    */30 * * * * wget -O - https://example.com/operation/lazy-actions?lt=|cat /home/myaccount/www/site/aToken.txt| >/dev/null 2>&1
    

    但是,上述解决方案未能运行cronjob。

    我使用编辑cronjobs crontab -e 在Ubuntu 16.04上使用nano

    2 回复  |  直到 6 年前
        1
  •  1
  •   Zak    6 年前

    这是一个快速的解决方案,它可以完全满足您的需要,而不需要复杂的一行程序:

    myaccount --你也可以把它放进你的 bin CRON sh 文件在中

    #!/bin/bash
    
    #simple cd -- change directory
    cd /home/myaccount/www/site/  
    
    #grab token into variable aToken
    aToken=`cat aToken.txt`  
    
    #simple cd -- move to wget directory
    cd /wherever/you/want/the/wget/results/saved 
    
    #Notice the $ -- This is how we let the shell know that aToken is a variable = $aToken
    #wget -O - https://example.com/operation/lazy-actions?lt=$aToken
    wget -q -nv -O /tmp/wget.txt https://example.com/operation/lazy-actions?lt=$aToken >/dev/null 2>/dev/null
    
    # You can writle logs etc etc afterward here.  IE
    echo "Job was successful" >> /dir/to/logs/success.log
    

    克罗恩

    */30 * * * * sh /home/myaccount/www/site/wget.sh >/dev/null 2>&1
    
        2
  •  1
  •   SaidbakR    6 年前

    Concatenate in bash the output of two commands without newline character ,我得到了以下简单的解决方案:

    wget -O - https://example.com/operation/lazy-actions?lt="$(cat /home/myaccount/www/site/aToken.txt)" >/dev/null 2>&1