linux - 使用 Crontab 连接文件输出文本

标签 linux cron concatenation cat cron-task

这个问题我已经跟进成功了,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/

相关文章:

linux - 将 python 3 安装到 venv 中?

java - 使用 Java + Cron 读取电子邮件

linux - 我的 cron 作业运行了几次

c - 在Linux(RedHat)中,C函数malloc_stats()与/proc/<pid>/stat常驻内存大小相比显示不同的值

c++ - 为什么在我的 Apache 2.2 模块中调用 ap_hook_type_checker 函数时 request_rec::filename NULL?

php - 如何根据条件制作出现管道

MySQL 我如何完成这个 select concat?

Java-gzip 解压缩的澄清

linux - 如何在 Windows Azure 上打开 EndPoints Linux 的所有端口

php - 查询数据库以获取时间戳相同且电子邮件地址相同的多行