问题描述
我正在尝试创建一个简单的 crontab,通过使用简单命令的输出填充它,每分钟创建一个名为 log.txt 的文件.现在这就是我放入 crontab 的内容:
I'm trying to create a simple crontab that creates a file called log.txt every minute by populating it with a simple command's output. Right now this is what I've put into my crontab:
* * * * * (/usr/bin/ls <pathToRandomDirectory) > log.txt
据我所知,这 5 个星号对应于每分钟".但是当我运行它时,没有创建 log.txt 文件.有什么我在这里想念的吗?
By my understanding, the 5 asterisks correspond to "every minute". But when I run this the log.txt file is not being created. Is there something I'm missing here?
另外,如果我不想在创建工作时收到一封电子邮件,我发现我需要输入以下行:
ALSO, if I didn't want to have an email sent to me whenever the job is created I found that I need to put the line:
>/dev/null 2>&1
在我的 crontab 文件中的某处.这到底去哪里了?在命令的末尾还是在单独的一行?
Somewhere in my crontab file. Where exactly does this go? At the end of the command or on a separate line?
推荐答案
必须为 log.txt 设置绝对路径.否则,它将在/中创建.
You have to put an absolute path for log.txt. Otherwise, it will be created in /.
此外,>/dev/null 2>&1
必须在句子的末尾.如果您希望消除 2(意味着错误),只需编写 2>/dev/null
.
Also, >/dev/null 2>&1
has to be at the end of the sentence. If you want the 2 (meaning the errors) to be dismissed, just write 2>/dev/null
.
然后,您的最终 cronjob 将是这样的:
Then, your final cronjob would be like this:
* * * * * /usr/bin/ls pathToRandomDirectory > /pathToRandomDirectory/log.txt 2>/dev/null
这篇关于crontab 创建一个新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!