Cron is running in home directory instead of file directory

与世无争的帅哥 提交于 2021-02-16 14:42:25

问题


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.

When I was troubleshooting why the dotenv gem is not working, I realised the following:

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

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.


回答1:


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.

Consider changing your crontab to:

* * * * * cd /home/myuser/ruby && ruby ./test.rb >> /home/myuser/ruby/output.log

Good luck!




回答2:


According to @Pawel Dawczak who left the answer in the comment.

the solution is to rewrite the statement in crontab as

* * * * * cd /home/myuser/ruby && ruby test.rb >> /home/myuser/ruby/output.log

Thanks!



来源:https://stackoverflow.com/questions/29341257/cron-is-running-in-home-directory-instead-of-file-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!