问题
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