Cron 在主目录而不是文件目录中运行 [英] Cron is running in home directory instead of file directory
问题描述
我关注了 stackoverflow 中的一些其他帖子,并使用 rvm cron setup
成功地设置了带有 RVM 的 cron,并向 crontab 文件注入了一些 ENV.
I followed some other posts in stackoverflow and successfully setup cron with RVM using rvm cron setup
and injected some ENV to the crontab file.
当我对 dotenv
gem 不起作用的原因进行故障排除时,我意识到以下几点:
When I was troubleshooting why the dotenv
gem is not working, I realised the following:
我将我的 test.rb
放在文件路径 /home/myuser/ruby/test.rb
中,并且我的 crontab
文件如图所示下面:
I placed my test.rb
in file path /home/myuser/ruby/test.rb
and had my crontab
file as shown below:
* * * * * ruby /home/myuser/ruby/test.rb >> /home/myuser/ruby/output.log
当我将 test.rb 的输出与 Dir.pwd
放在一起时.输出表明 rb 在 /home/myuser/
目录而不是 /home/myuser/ruby
目录中运行.
and when I puts the output of the test.rb with Dir.pwd
. The output states that the rb is run in the /home/myuser/
directory instead of /home/myuser/ruby
directory.
虽然我通过手动更改路径进行了修补程序.但我想知道为什么会这样.
While I had a hotfix by manually changing the path. But I wonder why it is the case.
推荐答案
默认情况下,用户的 cron 任务是从用户的主目录执行的.为了从正确的目录执行脚本,你必须cd"到它.
By default, cron tasks of a user are executed from the user's home directory. In order to execute the script from proper directory, you have to "cd" to it.
考虑将您的 crontab 更改为:
Consider changing your crontab to:
* * * * * cd /home/myuser/ruby && ruby ./test.rb >> /home/myuser/ruby/output.log
祝你好运!
这篇关于Cron 在主目录而不是文件目录中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!