Cron在主目录而不是文件目录中运行 [英] Cron is running in home directory instead of file directory
问题描述
我关注了stackoverflow中的其他文章,并使用 rvm cron setup
使用RVM成功设置了cron,并将一些ENV注入到crontab文件中.
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屋!