root cron not running - ruby-on-rails

I have a root user cron setup to run every one hour. I manually ran the rake task and it ran successfully as root user. But when i added this rake task as a cron, it doesn't work, I checked the /var/log/syslog and it seems to have the run the cron and also I redirected the stdout and stderr and examined the file, but the file seems to be empty. My guess is that there is an issue with running the cron as a root user. Has anyone face this problem before.?
Quick Update
In the stdout/stderr logs I'm getting a missing gems, I'm using RVM and I have installed my gems both globally and the gemset which I'm currently using.

Sounds like maybe rake can't be found in the default cron environment. Try first changing to the appropriate directory, and then this below, which will try to locate rake and then run it:
0 */1 * * * cd /app/directory; /usr/bin/env rake <task>
Also try adding setting a PATH that includes the path to rvm binaries:
PATH=/usr/local/rvm/bin;/usr/local/bin:/usr/bin:/bin
(Substitute /usr/local/rvm/bin with the path to rvm on your system)

Related

Cronjobs for Rails stopped working after switching to asdf

I recently changed from rvm to asdf.
Since then, I can't get cronjobs to run.
crontab -l
* * * * * /bin/bash -l -c 'cd /var/www/jobs/code && RAILS_ENV=production bundle exec rake cron_test' >> /tmp/cron_test_output 2>&1 /tmp/cron_error
gives me /tmp/cron_error: bundle: command not found. There is no output to stderr.
gem install bundler has not worked for the jobs user.
Not sure where to install bundler to make this work.
The rake tasks work when run independently. The cronjobs that are not rake tasks run. Cron is working.
Edit:
While the cronjobs were working before, they obviously can't now find the location of bundler. By changing bundle exec to the full path /home/jobs/.asdf/shims/bundler exec, I have managed to get this to work.
I have been using the whenever gem to create cron jobs for rake tasks. Turns out the easiest way to get around this problem is to make sure that the environment path is included in the whenever generated crontab.
within config/schedule.rb for the whenever gem, I added the following:
env :PATH, ENV['PATH']
and now everything is working as the cron job can now find the bundler.
Based on this issue from the whenever gem.
And this one too.

Can't execute Rails rake task with cron

I'm trying to run rake task with cron on Ubuntu with rvm
My crontab -l
* * * * * cd /media/sf_visa-tracker/ && /home/ruslan/.rvm/gems/ruby-2.1.0/bin/rake parse RAILS_ENV=production >> /var/log/visa-parse.log 2>&1
which rake says
/home/ruslan/.rvm/gems/ruby-2.1.0/bin/rake
And I keep getting this error in my log (/var/log/visa-parse.log)
/usr/bin/env: ruby_executable_hooks: No such file or directory
Also, if I run cd /media/sf_visa-tracker/ && /home/ruslan/.rvm/gems/ruby-2.1.0/bin/rake parse RAILS_ENV=production from my terminal, rake task works.
Pls help ;)
The simplest way to use rvm with cron is to use rvm's wrappers. Your shell sets up a whole bunch of rvm-related environment when it starts, and that's missing from your cron job. The wrappers are versions of the Ruby-related commands that take care of this for you.
In this case, if you have rvm installed to /usr/local, your cron job should look something like this:
* * * * * cd /media/sf_visa-tracker/ && /usr/local/rvm/wrappers/ruby-2.1.0/bin/rake parse RAILS_ENV=production >> /var/log/visa-parse.log 2>&1
You could also bundle up your rvm setup into a shell script that loads rvm before invoking rake; there's more details of both approaches in rvm's documentation on working with cron.
I struggled to get something that worked and seemed clean. Here's what I ended up doing in case it helps.
crontab
0 5 * * * /home/deploy/apps/myproject/shared/cron/daily_updates.sh
I put the script in myproject/shared since I'm using Capistrano and that seemed like a good place for things that would persist between releases.
shared/cron/daily_updates.sh
#!/usr/bin/env bash
# You will need to update this as you bump ruby versions
source /home/deploy/.rvm/environments/ruby-2.3.1#myproject
# Since I had a separate place for Rails specific environment variables (e.g. secrets, app keys)
# you should set the permissions on that file to be owner read/write only
source /home/deploy/.rails_env
cd /home/deploy/apps/myproject/current
rake RAILS_ENV=production daily_update:send_updates 2>&1

Clearing All Assets In GitLab

I want to use custom logo-white, logo-black, and favicon in my company installation of GitLab.
I've Googled my butt off and tried eveything I can find to clear these damn images and nothing seems to work.
Here's the only process that seems to run successfully, yet it does not remove the images:
bundle exec rake cache:clear RAILS_ENV=production
service gitlab stop
redis-cli FLUSHALL
bundle exec rake assets:precompile RAILS_ENV=production
service gitlab start
Then I clear my browser cache and go to the domain and there are the same damn images again!
I even deleted all the logo and favicon files I could find completely from the application, and yet somehow they are still there.
HELP!
There's a rake task assets:clean since version 6.0. So run bundle exec rake assets:clean RAILS_ENV=production from the command line and it will remove the assets.
After you nuke them, you'll probably want to run bundle exec rake assets:precompile RAILS_ENV=production to rebuild them.
I was able to combine some of the info here, and successfully replaced the logo using the following steps (note that some commands are slightly different using the latest version of gitlab, 7.11.4, on CentOS 7, so I am adding this as an answer in the hopes that it will be helpful to users with the new version):
Replace the appropriate images in /opt/gitlab/embedded/service/gitlab-rails/app/assets/images
Stop gitlab using sudo gitlab-ctl stop
Refresh the rails cache sudo gitlab-rake assets:clean RAILS_ENV=production followed by sudo gitlab-rake assets:precompile RAILS_ENV=production
Start gitlab using sudo gitlab-ctl start
For some reason I was getting permission errors in the precompile step. This was solved by changing the write permissions to /opt/gitlab/embedded/service/gitlab-rails/public/assets/ to be globally writable (a+w). It seems that gitlab-rake runs as the git user, even though I used sudo. So, after the steps above, I also changed the owner to root (sudo chown -R root:root /opt/gitlab/embedded/service/gitlab-rails/public/assets), and the permissions back using a-w.
I assume that upgrading gitlab will revert the logo back to the original one, but I haven't tried that yet.
I found the answer for anyone looking.
You also have to replace the images in
app/assets/images/

How to make crontab use the correct rake and refer to gems in vendor/bundle

I would like my cron job, which calls a rake task, to use the gems installed in either [rails_app]/vendor/bundle or [rails_app]/vendor/cache. Currently, my system gems are out-of-date, but I cannot update them for various reasons beyond my control.
I tried using bundle exec, but this does not seem to work. The error I get is
usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.18/lib/bundler/spec_set.rb:88:in 'materialize': Could not find rake-0.9.2.2 in any of the sources (Bundler::GemNotFound)
I believe this is because my system's rake is out-of-date. I tried using bundle exec rake _0.9.2.2_ [my_task] in the cron job, but the same error appeared
I tried the whenever gem; this produced the exact error.
So I guess I have two questions:
How do I make cron use the correct rake version? And
How do I point it to gems already in vendor/bundle or vendor/cache?
Note that the site works when deployed in Phusion Passenger.
While hunting a solution for the same problem I found this Thread with several tips.
the quick & easy way for me was to just run the cron command as login shell - like this:
* * * * * /bin/bash -l -c 'bundle exec rake _0.9.2.2_ [my_task]' 2>&1
the ending 2>&1 is just to be sure to receive errors in cron log. also see man bash for further info!

Cannot run rake with cron tasks

I am unable to run rake tasks with cron. The error it throws up is:-
/Library/Ruby/Gems/1.8/gems/bundler-1.2.0/lib/bundler/runtime.rb:199: warning: Insecure world writable dir /Library/Ruby/Gems/1.8 in PATH, mode 040777
/Library/Ruby/Gems/1.8/gems/bundler-1.2.0/lib/bundler/spec_set.rb:90:in `materialize': Could not find unf_ext-0.0.5 in any of the sources (Bundler::GemNotFound)
However, when I run the same rake task directly from the terminal, it works fine. In case of the cron job, as well as while running the rake task directly from the terminal, i cd into the (exact) same directory. Starting the rails server works as well.
What is the problem and how should I resolve this?
It could be an issue with your daemon user and your user when you're running it directly. Or it could be a problem with Ruby and/or your cron file commands.
Firstly, use this gem for making cron jobs with rake tasks:
https://github.com/javan/whenever
Then install RVM since you're using the standard ruby library provided by OSX and that can lead to some problems when you're developing various projects. This should fix the permissions error that you have displayed.

Resources