rake task in cron - ruby-on-rails-3

I have a shell script (/home/user/send_report.sh) that runs my rake task:
cd /home/user/rails/app
/home/user/.rvm/gems/ruby-1.9.2-p136/bin/rake report:send
When I run it in console sh /home/user/send_report.sh it works properly.
I am trying to make a cron task with my script: */10 * * * * sh /home/user/send_report.sh, but nothing happens! Rake task should send mail, but this does not happen.
Content of /var/log/cron.log:
Jun 2 21:40:01 ubuntu CRON[1253]: (user) CMD (sh /home/user/send_report.sh)
Jun 2 21:40:01 ubuntu CRON[1251]: (user) MAIL (mailed 240 bytes of output but got status 0x0001#012)
Please, help me to get the working rake script with crontab.

Apart from the fact that you should use /bin/sh, i don't see anything wrong on the cron job. When you run manually, you get the email as you said right ? It does not only work when you do it with cron ? It seems that it could be a misconfiguration of the email server or maybe that the mail server port is blocked ?

Problem was that RVM single-user installation doesn't supports cron tasks. Installed RVM as multi-user and crontab worked properly.

Related

Cron job permission denied

I'm running a python script called TGubuntu.py.
I used ls -l , and the permissions of the script are -rwxrwxrwx 1 ubuntu ubuntu 503 Jan 13 19:07 TGubuntu.py, which should mean that anyone can execute the file, right?
But I still get in the log /bin/sh: 1: /home/ubuntu/TestTG/TGubuntu.py: Permission denied for some reason.
When I run the script manually it works perfectly.
Any Ideas?
I put it in the sudo crontab like this
* * * * * /home/ubuntu/TestTG/TGubuntu.py
But even in the root (cron) mail log it says Permission Denied!
Couldn't figure out what the problem was, so I accomplished my goal using a different method.
I ran a python script that uses the schedule module to call my script. Then I just let the "Timer" run on screen indefinitely.

Cronjob Homestead not working

I've created a command to send automatic emails. When I do homestead ssh and I run php artisan emails.send an email arrives in my mailtrap.io account.
I've added this code to the kernel.php
$schedule->command('emails:send')->everyFiveMinutes();
I've put it at a 5 minute interval, just to make it easier to quickly test it.
I've ssh'd into Homestead and performed
php /home/vagrant/Code/soeptime/artisan schedule:run 1>> /dev/null 2>&1
then I exit homestead and I did homestead provision
However, there is nothing in the logs and I still haven't received an email, homestead is now running for more then 15 minutes.
You need to manually edit crontab:
First:
crontab -e
and then add
* * * * * php /home/vagrant/Code/soeptime/artisan schedule:run
From the Laravel docs:
Laravel provides a convenient way to schedule Cron jobs by scheduling
a single schedule:run Artisan command to be run every minute. The
schedule:run command will examine the job schedule defined in your
App\Console\Kernel class to determine which jobs should be run.
If you would like the schedule:run command to be run for a Homestead
site, you may set the schedule option to true when defining the site:
sites:
- map: homestead.test
to: /home/vagrant/code/Laravel/public
schedule: true
The Cron job for the site will be defined in the /etc/cron.d folder of the virtual machine.

how to run rake task in background in rails

This is my command
bundle exec rake resque:work QUEUE="*" --trace
I want to run this command on my server as a background process.
please help me.
A method I often use is:
nohup bundle exec rake resque:work QUEUE="*" --trace > rake.out 2>&1 &
This will keep the task running even if you exit your shell. Then if I want to just observe trace output live, I do:
tail -f rake.out
And you can examine rake.out at any time.
If you need to kill it before completion, you can find it with ps and kill the pid.
Just in case somebody finds this 4 years later, bundle has an elegant way of doing this now. For example if you want to run sidekiq in the background you can do:
bundle exec sidekiq -e production -d -L ./log/sidekiq.log
The -d daemonizes to run in the background, but you will also need to use the -L to provide a logfile, else bundler will refuse to run your command in the background (deamonize).
Tested with bundler version 1.15.4
Update Oct 2019.
While the command still works in general, the specific command above will no longer work for sidekiq 6.0+, you'll need to use Upstart or Systemd if you use Linux: https://github.com/mperham/sidekiq/wiki/Deployment#running-your-own-process

Rails 3 / whenever cron not firing

I am using whenever to fire a rake task every 5 minutes for my app.
schedule.rb:
every 5.minutes do
rake "audit",
:environment => 'development'
end
"whenever" in console:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /Users/john/Sites/rambler && RAILS_ENV=development bundle exec rake audit --silent'
"rake audit" in console works properly.
So all looks good .... except it doesn't work. Nothing happens every five minutes.
Is this because I am trying to run it in development / local?
Thanks!
You need to update your cron file every time you change it.
After you have addded your cron job do this:
whenever --update-crontab 'project_name'
Also I only found whenever working fine in production mode only.
UPDATE:
I have found that we can use whenever in development mode also. Just add
set :environment, "development"
set :output, {:error => "log/error.log", :standard => "log/cron.log"}
to your scehdule.rb file. ( The log one is optional but still you can use that for testing purpose)
Finally I have solved how to run the gem Whenever. It's working good on production, but not in development mode (I think that to working good in dev mode you must do some tricks).
see this answer for working in dev mode: Cron not working in Whenever gem
Then, these are the processes to do:
install the gem
write your scheduler.rb file
push to the remote server
login to the remote server (for example with ssh)
see if whenever is good uploaded by running in terminal: whenever
update whenever crontab by running: whenever --update-crontab
restart the server crontab (for example in ubuntu): sudo service cron restart
check if crontab are good implemented on the server: crontab -l
That is!

Why is my cron Job not running on OS X (configured by whenever gem)?

I have set up a cron job, using the great whenever gem.
every 1.minute do
runner "PeerReview.start_feedbacks"
end
I set up the cron job with:
whenever --set environment=development --update-crontab
The crontab file looks fine, it shows:
* * * * * /bin/bash -l -c 'cd /path_to_app/ && script/rails runner -e development '\''PeerReview.start_feedbacks'\'' >> log/cron_log.log 2>&1'
If I execute the runner, it works fine, however, the cron job doesn^t seem to work (also I don't see a log being created).
What am I missing?
(I'm working on Mac OS X, 10.6.6)
update
I think I identified the problem: The path name contains spaces, and this wasn't handled correctly by the whenever gem, the crontab was filled incorrectly (the needed backslashes are missing), so the cronjobs are executed, but the path for the command is wrong.
All above was correctly done.
The rails app was in a directory with spaces in the path names, those spaces were not escaped by the whenever gem, when setting up the crontab.