Cron在Ubuntu的工作 [英] Cron job in Ubuntu

查看:179
本文介绍了Cron在Ubuntu的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是很直接,但我似乎无法得到它工作,尽管通过谷歌,Stackoverflow和手册页阅读几个教程。



我创建

  crontab -l 
* * * * * /var/test/cron-test.sh

cron-测试文件是:

  echo$(date):cron job run> test.log 

等待很多分钟,我从未看到test.log文件。



我可以手动调用test.sh并得到输出& append。



我想知道我缺少什么?我也做这个作为根。我想知道我是否错过理解根的位置的东西?我的路径是否因为它附加了某种主目录而惹恼了?



谢谢!






UPDATE -----------------



似乎我不是跟随目录路径的东西。如果我将目录更改为root的主目录:

 #cd 

我看到我的输出文件test.log,每一分钟都打印出所有的日期。



我将更新我的问题是,我错过 - 理解/路径?是否有一个术语,我需要使用它从根目录开始?



干杯!



2 -----------------



好的,所以我得到了我错过的。



设置crontab的脚本工作正常。它是找到相对于根目录的文件。即:

  * * * * * /var/test/cron-test.sh 

但是没有相对于根目录设置cron-test.sh文件。因此,当root运行脚本时,它将其转储回root的主目录。我的想法是,因为脚本在/ var / test /中运行,该文件也将被转储在/ var / test /\".



,我需要在脚本文件中设置位置以正确转储它。

  echo$(date):cron job运行>> /var/test/test.log 

这很有效。


<您没有为test.log提供任何路径,因此它将在当前路径(默认情况下是用户的主目录)中创建。。


您应该更新脚本并提供完整路径,例如:

  echo$(date):cron job运行>> /var/log/test.log 


This should be pretty straight forward but I can't seem to get getting it working despite reading several tutorials via Google, Stackoverflow and the man page.

I created a cron to run every 1 min (for testing) and all it basically does is spit out the date.

crontab -l
* * * * * /var/test/cron-test.sh

The cron-test file is:

echo "$(date): cron job run" >> test.log

Waiting many minutes and I never see a test.log file.

I can call the "test.sh" manually and get it to output & append.

I'm wondering what I'm missing? I'm also doing this as root. I wonder if I'm miss-understanding something about root's location? Is my path messed up because it's appending some kind of home directory to it?

Thanks!


UPDATE -----------------

It does appear that I'm not following something with the directory path. If I change directory to root's home directory:

# cd

I see my output file "test.log" with all the dates printed out every minute.

So, I will update my question to be, what am I miss-understanding about the /path? Is there a term I need to use to have it start from the root directory?

Cheers!

UPDATE 2 -----------------

Ok, so I got what I was missing.

The script to setup crontab was working right. It was finding the file relative to the root directory. ie:

* * * * * /var/test/cron-test.sh

But the "cron-test.sh" file was not set relative to the root directory. Thus, when "root" ran the script, it dumped it back into "root's" home directory. My thinking was that since the script was being run in "/var/test/" that the file would also be dumped in "/var/test/".

Instead, I need to set the location in the script file to dump it out correctly.

echo "$(date): cron job run" >> /var/test/test.log

And that works.

解决方案

You have not provided any path for test.log so it will be created in the current path(which is the home directory of the user by default). You should update your script and provide the full path, e.g:

echo "$(date): cron job run" >> /var/log/test.log

这篇关于Cron在Ubuntu的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆