I am running a Rails server that periodically runs a rake task using whenever gem and cron.
I can run the rake task using these successfully. On my remote server, I am running that as the root. This second attempt also works.
RAILS_ENV=production rake fetch_myitems
// attempt 1
//attempt 2
/bin/bash -l -c 'cd /home/rails/AniKawaii && RAILS_ENV=production bundle exec rake fetch_animegifs --silent >> log/whenever.log 2>&1'
After debugging, this is what I ended up with as the source of error. I understand that this is related to database.yml. However I can deploy to my live production server successfully and calling RAILS_ENV=production rake fetch_myitems does not trigger this error so I am confused as to what to do.
PG::ConnectionBad: fe_sendauth: no password supplied
Crontab -l
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /home/rails/AniKawaii && RAILS_ENV=production bundle exec rake fetch_animegifs --silent >> log/whenever.log 2>&1'
For some reason in /etc/default/unicorn file the APP_DATABASE_PASSWORD does not get updated even if I reload nginx along with unicorn.
My fix was to manually override the password in database.yml.
My guess is that your database.yml file looks something like:
production:
adapter: postgresql
encoding: unicode
database: test-postgres_production
pool: 5
username: rails
password: <%= ENV['PGPASS'] %>
Meaning that the password is obtained from an environment variable. Your cronjob should look something like:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /home/rails/AniKawaii && RAILS_ENV=production PGPASS=your_database_password_here bundle exec rake fetch_animegifs --silent >> log/whenever.log 2>&1'
Hope that sorts you.
Related
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
I'm struggling with starting sidekiq remotely with a custom v2 capistrano task:
namespace :sidekiq do
desc "Start sidekiq"
task :start do
run "cd #{current_path} && bundle exec sidekiq --version"
run "cd #{current_path} && bundle exec sidekiq --environment production --daemon --config config/sidekiq.yml && echo OK"
end
end
Output:
* 2018-01-05 11:40:51 executing `sidekiq:start'
* executing "cd /home/deploy/applications/xxx/current && bundle exec sidekiq --version"
servers: ["198.58.110.211"]
[198.58.110.211] executing command
** [out :: 198.58.110.211] Sidekiq 5.0.5
** [out :: 198.58.110.211]
command finished in 1424ms
* executing "cd /home/deploy/applications/xxx/current && bundle exec sidekiq --environment production --daemon --config config/sidekiq.yml && echo OK"
servers: ["198.58.110.211"]
[198.58.110.211] executing command
** [out :: 198.58.110.211] OK
command finished in 1128ms
I can confirm I'm getting the environment (rbenv & bundler correctly) as the first run cmd shows. But unexpectedly the sidekiq task starts and dispersal into obliviom: 1) tmp/pids/sidekiq.pid gets initialized but the process not exists and 2) logs/sidekiq.log gets created but only with the header:
# Logfile created on 2018-01-05 11:34:09 -0300 by logger.rb/56438
If I remove the --daemon switch I get the process running perfectly, but of course the capistrano deploy task never ends and when I do CTRL+C sidekiq closes.
If I just ssh into the remote and execute the command (replacing current_path obviously) it works perfectly.
I've tried almost everything I can imagine: not using a config.file, using RAILS_ENV instead of --environment, etc.
As the "&& echo OK" shows, the command is not returning an error.
Capistrano is using "/bin/bash --login -c 'cd /home/deploy/applications/microgestion/current && bundle exec sidekiq --environment production --daemon --config config/sidekiq.yml'" as far as I can tell to run the command.
Ruby v2.3.3, Capistrano 2.15.5, Sidekiq 5.0.5, Rails 4.0.12
Solved it by adding && sleep 1 at the end as explained here: http://blog.hartshorne.net/2013/03/capistrano-nohup-and-sleep.html.
desc "Start sidekiq"
task :start do
run "cd #{current_path} && bundle exec sidekiq --environment production --daemon --config config/sidekiq.yml && sleep 1"
end
Thanks #user3309314 for pointing me in the correct direction.
If you use plain Capistrano to daemonize Sidekiq, any crash will lead to downtime. Don't do this. You need to use a process monitor that will restart the Sidekiq process if it dies. Use systemd, upstart and/or Foreman as explained in the docs.
https://github.com/mperham/sidekiq/wiki/Deployment#running-your-own-process
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'
I am having an issue with using rubber, whenever I try cap rubber:bootstrap and had a staging instance, it always stuck on this error.
* executing "sudo -p 'sudo password: ' bash -l -c 'cd /mnt/localstore-production/releases/20120519213905 && RUBBER_ENV=production RAILS_ENV=production ./script/rubber config --force --file=\"role/graphite_server\"'"
servers: ["production.localstore.com"]
[production.localstore.com] executing command
** [out :: production.localstore.com] Instance not found for host: ip-10-2-118-252
** [out :: production.localstore.com]
command finished in 5849ms
failed: "/bin/bash -l -c 'sudo -p '\\''sudo password: '\\'' bash -l -c '\\''cd /mnt/localstore-production/releases/20120519213905 && RUBBER_ENV=production RAILS_ENV=production ./script/rubber config --force --file=\"role/graphite_server\"'\\'''" on production.localstore.com
Actually the issue was that I changed the static IP of the instance from the AWS Console, so why the host info of the instance changed some how.
So I use this command cap rubber:referesh to refresh every thing and then bootstrap the instance and it solved my problem.
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.