Running raketask with cron - ruby-on-rails

all!
I try to rane rake-task in cron. Craken generate for me such crontab:
*/5 * * * * cd /usr/local/www/vhosts/proj/www/current && /usr/local/www/vhosts/proj/www/shared/bundle/ruby/1.8/bin/rake --silent RAILS_ENV=production my:rake_task >> ./tmp/log.log 2>&1
If I try to run this command manually, it works fine
cd /usr/local/www/vhosts/proj/www/current && /usr/local/www/vhosts/proj/www/shared/bundle/ruby/1.8/bin/rake --silent RAILS_ENV=production my:rake_task >> ./tmp/log.log 2>&1
But after it runned by cron, I get in log:
env: ruby18: No such file or directory
Prompt, please, where am I wrong?

Please check the environment variables (like PATH and maybe any other which are required by Ruby). Add the environment variables and the above commands into a shell script and retry.

Related

Rake command works in terminal but not in crontab

I try to execute this crontab line :
*/1 * * * * /bin/bash -l -c 'source $HOME/.bash_profile && cd /home/uservm/RubymineProjects/ && RAILS_ENV=development bundle exec rake accr:generate_pdf'
If I execute cd /home/uservm/RubymineProjects/ && RAILS_ENV=development bundle exec rake accr:generate_pdf in terminal it works perfectly but not in cron
This is because crontab runs in different environment than the normal user(when you type in terminal). So you need to specify your ruby installation path:
Run:
$ which ruby
Copy your ruby path and then, change your crontab to
*/1 * * * * /bin/bash -l -c 'source $HOME/.bash_profile && cd /home/uservm/RubymineProjects/ && /path/to/ruby RAILS_ENV=development bundle exec rake accr:generate_pdf
If this didn't solve then run $ which bundle and replace ruby path with this bundle path and see if it works.
This should solve your problem. For more: See this

Crontab throws error

I've below command in crontab, when I run this command in terminal it works fine but when I run this in crontab am getting the following error
* * * * * cd /home/path/application && RAILS_ENV=development ./bundle exec rake namespacefolder:rake_file
Error:
bundler: command not found: rake
Install missing gem executables with `bundle install`
someone please help.
Cron passes only minimal set of environment variables to your jobs. See here!
Add -lc option to bash for cron execution to use your login environment and set environment path at the top of your crontab.
PATH=$PATH:/usr/bin:/bin:/usr/local/bin
* * * * * /bin/bash -lc "cd ~/home/path/application && RAILS_ENV=development bundle exec rake namespacefolder:rake_file"
Try this one if not solved with bundle
* * * * * /bin/bash -l -c "cd ~/home/path/application && RAILS_ENV=development bundle exec rake namespacefolder:rake_file"
/bin/bash
Specify which program to use for executing the command
-c
Read and execute commands from the first non-option argument after processing the options, then exit. Any remaining arguments are assigned to the positional parameters, starting with $0.
-l
Make this shell act as if it had been directly invoked by login. When the shell is interactive, this is equivalent to starting a login shell with ‘exec -l bash’. When the shell is not interactive, the login shell startup files will be executed. ‘exec bash -l’ or ‘exec bash --login’ will replace the current shell with a Bash login shell. See Bash Startup Files, for a description of the special behavior of a login shell.
Refer this documentation here

Whenever Gem and cron Job

I followed the instruction on Whenever gems website, and tried following it in both development and on production mode on server.
set :output, "/file/to/path/cron.log"
every 1.day, at: '11:59 pm' do
command "sudo touch /file/to/path"
runner "Location.transfer_data",:environment => 'production'
end
It doesn't create the file(which is a test for now since I have no data on production).
I have spent hours trying to debug it and crontab -l gives me this output
59 23 * * * /bin/bash -l -c 'sudo touch /file/to/path >> /file/to/path/cron.log 2>&1'
59 23 * * * /bin/bash -l -c 'cd /var/www/name && script/rails runner -e production '\''Location.transfer_data'\'' >> /file/to/path/cron.log 2>&1'
Regards,
Babar Rehman
I solved the issue, don't know which one solved it in particular but these were the step that I took
Added job name to whenever --update-cron command
whenever --update-cron jobName
Restarted the cron service
sudo service cron restart
Gave full access rights to the log file
sudo chmod 777 /path/to/file.log
Hope it will come in handy for others

Rake Cron job error

0/5 * * * * /bin/bash -l -c 'cd /home/mss/ruby/example && RAILS_ENV=development /usr/local/bin/bundle exec rake check_me_out --silent >> /tmp/cron_log.log 2>&1'
Above cron throws an error "bash: bundle: command not found..."
The command works just fine from the command line
Any help will be appreciated.
Okay so I got this working. Cron does not load the profile settings. I had to load the bash_profile as part of the commands and now it works.
0/5 * * * * /bin/bash -l -c 'source ~/.bash_profile && cd /home/mss/ruby/example && RAILS_ENV=development bin/rake check_me_out --silent >> /tmp/cron_log.log 2>&1'
I faced the same problem.
I had solved it when set correct RVM path in CRON:
** * * * /bin/bash -l -c 'cd /home/alex/Projects/my_app && source ~/.bash_profile && rvm use ruby-1.9.3-p194-perf && bundle exec rake RAILS_ENV=development my_tasks --silent >> /tmp/cron_log.log 2>&1'
I also had the same issue when I set up cron on the AWS EC2 server. It was resolved by setting bundle path explicitly in the config/schedule.rb file.
set :bundle_command, "/usr/local/bin/bundle exec"
This creates entry in the cron like:
30 1 * * * /bin/bash -l -c 'cd <app_path> && RAILS_ENV=beta /usr/local/bin/bundle exec rake 'task_name' --silent >> log/cron.log 2>&1'

Rails hourly cron jobs using Whenever fails to run hourly

On Redhat, using Whenever. My cron jobs fail to run hourly. I need help as to why.
Schedule.rb
every 1.hours do
rake "deathburrito:all", :environment => "development"
rake "bamboo:all", :environment => "development"
rake "jira:grab_data", :environment => "development"
end
Crontab -l
0 * * * * /bin/bash -l -c 'cd /var/www/qadashboard && RAILS_ENV=production bundle exec rake deathburrito:all --silent'
0 * * * * /bin/bash -l -c 'cd /var/www/qadashboard && RAILS_ENV=development bundle exec rake bamboo:all --silent'
0 * * * * /bin/bash -l -c 'cd /var/www/qadashboard && RAILS_ENV=development bundle exec rake jira:grab_data --silent'
Can anyone help me? I am not even sure what else I should be checking.
Add
MAILTO=your#email.com
to your crontab. Then enjoy the error reports from cron.
If that won't solve the issue, post the error report here.
bundle will have to be in that subshell's path. Try specifying a full-blown /usr/bin/bundle (or whatever it is).
Append log to config/schedule.rb
set :output, "/var/log/cron"
and create this file 'cron' in /var/log and give it write permission.
Execute
bundle exec whenever --update-crontab
sudo /etc/init.d/cron restart
To see the logs:
tail -f /var/log/cron
will give you more insight on the error.

Resources