Cron job not running using Capistrano & whenever - ruby-on-rails

I'm stumped on this one. I am using capistrano and whenver gems to manage my builds to prod. Cron is being setup correctly on prod. When I look at 'crontab -e' I see...
# Begin Whenever generated tasks for: nso
10 * * * * /bin/bash -l -c 'cd /home/user/nso/releases/20140130161552 && RAILS_ENV=production bundle exec rake send_reminder_emails --silent >> /var/log/syslog 2>&1'
...this looks correct. In /var/log/syslog I see...
Feb 3 16:10:01 vweb-nso CRON[32186]: (user) CMD (/bin/bash -l -c 'cd /home/user/nso/releases/20140130161552 && RAILS_ENV=production bundle exec rake send_reminder_emails --silent >> /var/log/syslog 2>&1')
Feb 3 16:10:01 vweb-nso postfix/pickup[31636]: 8A67B805C7: uid=1001 from=<user>
Feb 3 16:10:01 vweb-nso postfix/cleanup[32191]: 8A67B805C7: message-id=<20140203211001.8A67B805C7#vweb-nso>
Feb 3 16:10:01 vweb-nso postfix/qmgr[31637]: 8A67B805C7: from=<user#abtech.edu>, size=712, nrcpt=1 (queue active)
Feb 3 16:10:01 vweb-nso postfix/local[32193]: 8A67B805C7: to=<user#abtech.edu>, orig_to=<user>, relay=local, delay=0.03, delays=0.02/0/0/0, dsn=2.0.0, status=sent (delivered to mailbox)
Feb 3 16:10:01 vweb-nso postfix/qmgr[31637]: 8A67B805C7: removed
Everything looks OK there..no?
Addtionally I can manually run the command. At the command prompt if I do.
/bin/bash -l -c 'cd /home/user/nso/releases/20140130161552 && RAILS_ENV=production bundle exec rake send_reminder_emails --silent >> /var/log/syslog 2>&1'
...I get my reminder emails as I should. What am I missing?

So this ended up being my issue...
Rails cron whenever, bundle: command not found
Once I added env :PATH, ENV['PATH'] to the top of config/schedule.rb everything worked as advertised!

Related

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

How to set Timezone on whenever gem cronjob

I am using whenever gem + capistrano to automate my cronjob generation whenever I deploy my app to the live server. Currently the cron that being generated by whenever looks like this(without the timezone "TZ") :
30 20 * * * /bin/bash -l -c 'cd /home/deploy/apps/myapp/releases/20160123202716 && RAILS_ENV=production bundle exec rake overdue_payments --silent >> /home/deploy/apps/myapp/releases/20160123202716/log/cron.log 2>&1'
My question is, how do I make whenever generate this line together with the timezone (TZ="Europe/London"), so that it will looks like this:
30 20 * * * TZ="Europe/London" /bin/bash -l -c 'cd /home/deploy/apps/myapp/releases/20160123202716 && RAILS_ENV=production bundle exec rake overdue_payments --silent >> /home/deploy/apps/myapp/releases/20160123202716/log/cron.log 2>&1'
Hope somebody could help..thanks! :)
You can override the default template for jobs like this:
set :job_template, "TZ=\"Europe/London\" bash -l -c ':job'"
Simply define the variable on its own line within your crontab. see man page crontab(5)
TZ=Europe/London
30 20 * * * /bin/bash -l -c 'cd /home/deploy/apps/myapp/releases/20160123202716 && RAILS_ENV=production bundle exec rake overdue_payments --silent >> /home/deploy/apps/myapp/releases/20160123202716/log/cron.log 2>&1'

Duplicate process in cron job using Whenever gem for Rails

Using Rails 3.2.21, whenever gem. This is the list of my crontab:
Begin Whenever generated tasks for: abc
0 * * * * /bin/bash -l -c 'cd /home/deployer/abc/releases/20141201171336 &&
RAILS_ENV=production bundle exec rake backup:perform --silent'
Here's the output when the scheduled job is run:
deployer#localhost:~$ ps aux | grep rake
deployer 25593 0.0 0.0 4448 764 ? Ss 12:00 0:00 /bin/sh -c /bin/bash -l -c
'cd /home/deployer/abc/releases/20141201171336 && RAILS_ENV=production bundle exec rake
backup:perform --silent'
deployer 25594 0.0 0.1 12436 3040 ? S 12:00 0:00 /bin/bash -l -c cd
/home/deployer/abc/releases/20141201171336 && RAILS_ENV=production bundle exec rake
backup:perform --silent
deployer 25631 69.2 4.4 409680 90072 ? Sl 12:00 0:06 ruby /home/deployer/abc/
shared/bundle/ruby/1.9.1/bin/rake backup:perform --silent
deployer 25704 0.0 0.0 11720 2012 pts/0 S+ 12:00 0:00 grep --color=auto rake
Notice the the top 2 processes are actually similar processes. Are they running 2 same jobs concurrently? How do I prevent that?
deployer 25593 0.0 0.0 4448 764 ? Ss 12:00 0:00 /bin/sh -c /bin/bash …
deployer 25594 0.0 0.1 12436 3040 ? S 12:00 0:00 /bin/bash …
Notice the the top 2 processes are actually similar processes. Are they running 2 same jobs concurrently?
No, they aren't. The first is a /bin/sh that started the second, the crontab command /bin/bash …. Most probably /bin/sh is just waiting for termination of /bin/bash and not running again before /bin/bash … has finished execution; you can verify this with e. g. strace -p 25593.
Check your scheduled.rb for a duplicate entry, if you find then remove and deploy.
If there is no duplicate entry in scheduled.rb then you need to remove/comment it from cron tab.
To delete or comment jobs in cron take a look at https://help.1and1.com/hosting-c37630/scripts-and-programming-languages-c85099/cron-jobs-c37727/delete-a-cron-job-a757264.html OR http://www.esrl.noaa.gov/gmd/dv/hats/cats/stations/qnxman/crontab.html

cronjob with rails - whenever gem is doing nothing, why? (development mode)

I am trying to auto backup my database every night. I want to use the whenever gem and my application runs still in development environment.
i used wheneverize . command in my app folder to create the config/schedule.rb file, swhcih looks like this:
set :output, "/User/eveadmin/rails/feebacker/log/cron_log.log"
set :environment, "development"
every 1.day, :at => '2:30 am' do
dump_path = "/User/eveadmin/rails/db_backups/dump#{Date.today.to_s}"
command "sudo mysqldump -u user -p secretpasswort feedbacker_development > #{dump_path}.sql"
command "tar -zcvf #{dump_path}.tar.gz #{dump_path}.sql"
command "rm #{dump_path}.sql"
runner "Storage.store_dump '#{dump_path}.tar.gz'"
end
After deploying it to my server, also currently running in development mode, I used the command
whenever --update-crontab feedbacker --set environment=development
After this my crontab looks like the following:
# Begin Whenever generated tasks for: feedbacker
30 2 * * * /bin/bash -l -c 'sudo mysqldump -uroot -prootz feedbacker_development > /User/eveadmin/rails/db_backups/dump2011-07-07.sql >> /User/eveadmin/rails/feebacker/log/cron_log.log 2>&1'
30 2 * * * /bin/bash -l -c 'tar -zcvf /User/eveadmin/rails/db_backups/dump2011-07-07.tar.gz /User/eveadmin/rails/db_backups/dump2011-07-07.sql >> /User/eveadmin/rails/feebacker/log/cron_log.log 2>&1'
30 2 * * * /bin/bash -l -c 'rm /User/eveadmin/rails/db_backups/dump2011-07-07.sql >> /User/eveadmin/rails/feebacker/log/cron_log.log 2>&1'
30 2 * * * /bin/bash -l -c 'cd /Users/thomasmaximini/Sites/rails_projekte/feedbacker && script/rails runner -e development '\''Storage.store_dump '\''\'\'''\''/User/eveadmin/rails/db_backups/dump2011-07-07.tar.gz'\''\'\'''\'''\'' >> /User/eveadmin/rails/feebacker/log/cron_log.log 2>&1'
# End Whenever generated tasks for: feedbacker
It simply does nothing, no cron.log is generated, no backups are done, nothing. So I dont know what I am doing wrong as there are no errors raised to debug. I hope anybody can help, thanks.

Whenever ruby on rails

In my schedule:
every 10.minutes do
runner "Model.method"
end
Whenever created this in my crontabs
0,10,20,30,40,50 * * * * /bin/bash -l -c 'cd /home/projects/Monitoring && script/rails runner -e development '\''Model.method'\'''
I tried to run the command in my console and it works. Why does it not work automaticly, i am going insane!
In my syslog
Mar 11 11:38:01 UbuntuRails CRON[20050]: (ruben) CMD (/bin/bash -l -c 'cd /home/projects/Monitoring && script/rails runner -e development '\''Ping.check_pings'\''')
Mar 11 11:38:01 UbuntuRails CRON[20048]: (CRON) info (No MTA installed, discarding output)
Mar 11 11:38:01 UbuntuRails CRON[20047]: (CRON) error (grandchild #20050 failed with exit status 1)
Mar 11 11:38:01 UbuntuRails CRON[20047]: (CRON) info (No MTA installed, discarding output)
I am on Ubuntu 10.10 and had the same problem.
Turns out the -l option does not load the environment as expected, but -i does. (see this issue)
As the issue thread states, the fix is to edit your schedule.rb and add:
set :job_template, "/bin/bash -i -c ':job'"
Cheers

Resources