Cron is running in home directory instead of file directory - ruby

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.

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!

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!

Related

How to run a script from crontab and avoid "LoadError"

I want run my script from the crontab on Mac OS, but I'm getting an error:
ruby: Operation not permitted -- /Users/vitalii/Desktop/Home/update/update.rb (LoadError)
My preferences for the cron task and settings are created using rvm cron setup:
#sm start rvm
PATH="/Users/vitalii/.rvm/gems/ruby-2.4.1/bin:/Users/vitalii/.rvm/gems/ruby-2.4.1#global/bin:/Users/vitalii/.rvm/rubies/ruby-2.4.1/bin:/Users/vitalii/.rvm/gems/ruby-2.4.1/bin:/Users/vitalii/.rvm/gems/ruby-2.4.1#global/bin:/Users/vitalii/.rvm/rubies/ruby-2.4.1/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/Applications/Postgres.app/Contents/Versions/latest/bin:/Users/vitalii/.rvm/bin"
GEM_HOME='/Users/vitalii/.rvm/gems/ruby-2.4.1'
GEM_PATH='/Users/vitalii/.rvm/gems/ruby-2.4.1:/Users/vitalii/.rvm/gems/ruby-2.4.1#global'
MY_RUBY_HOME='/Users/vitalii/.rvm/rubies/ruby-2.4.1'
IRBRC='/Users/vitalii/.rvm/rubies/ruby-2.4.1/.irbrc'
RUBY_VERSION='ruby-2.4.1'
#sm end rvm
* * * * * ruby /Users/vitalii/Desktop/Home/update/update.rb >> /Users/vitalii/Desktop/logfile.txt 2>&1
I gave each file the rights to execute with chmod 777, but there are no changes and the error is repeated.
The contents of the file update.rb are
puts 'Hello, World!!!'
Can someone tell me what's going on and what I'm doing wrong ?
I found solution, since I use Mojave.
I need to make additional settings in the system. Who would have thought ...
enter link description here
This turns out to be a problem, since you need to allow permissions for cron.
And correct run command for crontab -e, this
* * * * * /bin/bash -l -c 'ruby /Users/vitalii/Desktop/Home/update/update.rb'
There is a little difference when setting crontab job via crontab -e or putting them in /etc/crontab
$ crontab -e
# m h dom mon dow command
* * * * * echo ok
$ nano /etc/crontab
# m h dom mon dow user command
* * * * * root echo ok
Is this the case?

Crontab dont find/recognize path

I'm using a osx and I created a ruby script in the path: /Users/diogo/workspace/outros/crawler_trf with name get_news.rb
So I tried to execute it via crontab with the following line: */1 * * * * 'ruby /Users/diogo/workspace/outros/crawler_trf/get_news.rb' > /tmp/crawler_trf.out and I've got the error: /bin/sh: ruby /Users/diogo/workspace/outros/crawler_trf/get_news.rb: No such file or directory
I really had searching for my answers for long but I didn't found nothing.
cron is feeding your command:
'ruby /Users/diogo/workspace/outros/crawler_trf/get_news.rb' > /tmp/crawler_trf.out
to /bin/sh as-is but there is no 'ruby /Users/diogo/...' command, there probably is a ruby command that can take /Users/diogo/... as an argument though so drop the quotes:
*/1 * * * * ruby /Users/diogo/workspace/outros/crawler_trf/get_news.rb > /tmp/crawler_trf.out
You might want to include the full path to ruby as well or at least make sure there's a PATH setting in your crontab.

Running ruby selenium scripts on Cronjob - Chrome

I am trying to run a selenium ruby script on Chrome through crontab but don't see anything run.
I tried the following as suggested in other questions:
crontab -u shamanth -e
Added the following
MAILTO=""
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
export DISPLAY=:0 google-chrome
47 13 * * * /usr/bin/ruby ~/Desktop/script.rb
From the cron logs /var/log/syslog, I see that the script has started (I put some print messages in the beginning of the script) but I don't see chrome launching.
Permissions have been given to the script file.
Most likely the problem is with the path you are setting: crontab works in a very limited environment, where paths like ~ are not understood.
For that, instead of saying
* * * * * /usr/bin/ruby ~/Desktop/script.rb # NO!
# ^
say
* * * * * /usr/bin/ruby /home/your_user/Desktop/script.rb # YES!
# ^^^^^^^^^^^^^^^

#!/usr/bin/env ruby is not found in cron

I have a simple ruby script, hello.rb:
#!/usr/bin/env ruby
puts 'hello'
It runs ok at the command line:
# /usr/local/src/hello/hello.rb
hello
However, if I put it in cron:
* * * * * /usr/local/src/hello/hello.rb >> /usr/local/src/hello/hello.log 2>&1
There are errors in the log file:
/usr/bin/env: ruby: No such file or directory
/usr/bin/env: ruby: No such file or directory
...
/usr/bin/env: ruby: No such file or directory
/usr/bin/env ruby runs ok at command line though:
# /usr/bin/env ruby -v
ruby 1.8.7 (2012-10-12 patchlevel 371) [i686-linux]
How to fix the env error for cron?
The problem is that the environment isn't what you expect.
You don't say whether the cron is running as your user, or as root, but, in either case, you can test to see what the environment looks like by adding another cron entry of:
* * * * * /usr/bin/env > /path/to/your/home/directory/env.txt
Let that run once, then pull it out, and look at the file.
Instead of using /usr/bin/env to try to find a Ruby to run your code, define the Ruby explicitly:
* * * * * /path/to/the/ruby/you/want /usr/local/src/hello/hello.rb >> /usr/local/src/hello/hello.log 2>&1
You can figure out which Ruby you want by using:
which ruby
Alternately, instead of relying on /usr/bin/env in your #! line, define your Ruby there.
Using /usr/bin/env ruby in your code is a convenience when you're using something like RVM or rbenv, and switching between versions of Ruby. It's not a good choice when you're putting something into "production", whether it's on your machine in your own account, or on a production host running as root.
If you are on Linux or Mac OS, try man 5 crontab for more information. Also, "Where can I set environment variables that crontab will use?" should be very useful.
env searches only in the existing PATH variable. crond creates the process that is run as your user name. So the PATH is minimal. You have to set up your environment variables in the script itself

Can the whenever gem preserve existing lines in a crontab file?

I am using:
Ruby 1.9.2
whenever 0.7.2
capistrano 2.9.0
capistrano-ext 1.2.1
I am using whenever in conjunction with Capistrano on deploys to manage my crontab files.
I noticed that it completely rewrites my crontab files each time.
I'd like to be able to set environment variables in cron to control PATH and MAILTO settings, which are regular cron environment variables.
Is there a way to make whenever not overwrite the entire crontab file, so that I can add customizations to my crontab file and be sure that they will persist?
Yes, you can do this. You'll just need to assign an identifier to the task being written to crontab:
whenever --update-crontab some_identifier_name
It will generate an entry in crontab like this:
# Begin Whenever generated tasks for: some_identifier_name
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /var/www/test/releases/20120416183153 && script/rails runner -e production '\''Model.some_method'\'' >> /tmp/cron_log.log 2>&1'
# End Whenever generated tasks for: some_identifier_name
Then whenever you call the command above it will only update where it finds the identifier you specified.

Resources