Crontab throws error - ruby-on-rails

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

Related

How te restart the sidekiq when it crushes down?

I started the sidekiq by
bundle exec sidekiq -d -L log/sidekiq.log -C config/sidekiq.yml -e production
Sometime, Sidekiq was crushed, busy=0 and enqueue > 0.
How can I setup the sidekiq to restart after crushed/stopped?
You can use some automated packages, or write your own bash script to do that.
I personally prefer writing my own scripts so let me explain that:
Write a script that executes sidekiq if not running already
Write a cronjob to execute that script every minute
Note that this method is not instant, meaning that you might have a downtime up to 1 minute, since the cronjob works every minute. So if your project is sensitive on that, you might want to use one of the process management tools such as monit or god.
Your bash script should contain your command,
bundle exec sidekiq -d -L log/sidekiq.log -C config/sidekiq.yml -e production
But make sure you are using absolute paths if you are going to store your script outside your project directory.
Here is a helpful topic about writing the shell script to check whether the process already exists.
To run the script every minute, do the following:
Go to your terminal
Type crontab -e
Append * * * * * /bin/bash -l -c 'cd /PATH/TO/YOUR/DIR && sh SCRIPTNAME.sh'
Save and exit editor
By doing this, you are telling your computer to execute your script every minute.
Usually people prefer writing only * * * * * /path/to/script.sh to their crontab but this fails in some circumstances.
Hope this helps.

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

Upstart task generated by foreman doesn't find file?

I used foreman to export my Procfile to an upstart task.
Procfile:
web: bundle exec rails server
websocket: bundle exec rails runner websocket-server/em_websocket.rb
One of the upstart tasks (they are very alike and fail with the same error):
start on starting app-web
stop on stopping app-web
respawn
env PORT=5000
setuid app
chdir /var/www/app
exec bundle exec rails server
And the error (I got it via dmesg):
[35207.676836] init: Failed to spawn app-websocket-1 main process: unable to execute: No such file or directory
[35207.679577] init: Failed to spawn app-web-1 main process: unable to execute: No such file or directory
When I switch to the app user, I am actually able to run bundle exec rails server from the given directory.
Is there any way to pin down the error a little more? I didn't find any related logs in /var/log/upstart/.
If you installed ruby via RVM it may be possible that the init is run before the rvm script runs. Did you try using absolute references to the bundle bin?
whereis bundle
to obtain it
RVM was apparently not initialized or is not available in the upstart enviroment. Luckily rvm has wrappers for this case: https://rvm.io/integration/init-d
You can run bundle in another way.
Instead of:
web: bundle exec rails server
You need to run:
web: bash -c '~/.rvm/bin/rvm default do bundle exec rails server'
Note: ~/.rvm/bin/rvm - can be replaced with actual path of rvm installation on your server.
Upstart commands require sudo privileges for the underlying user. Have you considered defining some form of passwordless sudo privileges for your app user to run the rails application service restarts?
e.g In Ubuntu creating a new sudoer definition under /etc/sudoers.d/?
username ALL=(ALL) NOPASSWD:ALL
Once defined 'username' should be able to run the rails app via sudo service 'appname' stop|start|restart.
Here is an explanation for providing the sudo privileges to the user. My Capistrano deployment contains a foreman export definition as below -
namespace :foreman do
desc 'Export the Procfile to Ubuntu upstart scripts'
task :export do
on roles(:app) do |host|
log_path = shared_path.join('log')
within release_path do
execute :mv, ".env .envbkup"
execute :echo, "'RACK_ENV=#{fetch(:deploy_env)}' >> .env"
execute :echo, "'RAILS_ENV=#{fetch(:deploy_env)}' >> .env"
execute :bundle, "exec foreman export upstart #{shared_path}/init -a #{fetch(:application)} -u #{host.user} -l #{log_path}"
execute :rm, ".env"
execute :mv, ".envbkup .env"
as :root do
execute :cp, "#{shared_path}/init/* /etc/init/"
end
end
end
end
This capistrano definition is invoked from the deploy_env.rb 'after' action.

Running raketask with cron

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.

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