这个问题我已经跟进成功了,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。
但是,访问 token 记录在 /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。
我在 Ubuntu 16.04 上使用 nano 使用 crontab -e
编辑 Cronjobs
最佳答案
这是一种快速解决方案,无需复杂的单行代码即可完全满足您的需求:
在您的 myaccount
中创建此文件 -- 如果您愿意,您也可以将其放入您的 bin
目录中,只需记住您放置它的位置,以便您可以从你的 CRON
。还要确保用户有权读/写 sh
文件所在的目录
wget.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
然后就像您已经在做的那样,用您的 CRON
简单地调用这个文件。
*/30 * * * * sh /home/myaccount/www/site/wget.sh >/dev/null 2>&1
关于linux - 使用 Crontab 连接文件输出文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50933822/