1

I am trying to make a simple cron job running this task: Here is my schedule.rb:

set :environment, "staging"
set :path, "/var/www/my_app/current"
every 2.minutes do
  # specify the task name as a string
  rake 'cron_test'
end

Here is the task:

task :cron_test => :environment do
  out_file=File.new("cron_test.txt", "w")
  out_file.puts(Time.now.to_s)
  out_file.close
end

I tried to do what is advised on this page but nothing works: Cron job not working in Whenever gem

When I run crontab -l :

# Begin Whenever generated tasks for: /var/www/my_app/current/config/schedule.rb
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /bin/bash -l -c 'cd /var/www/my_app/current && RAILS_ENV=staging bundle exec rake cron_test'

# End Whenever generated tasks for: /var/www/my_app/current/config/schedule.rb

When I run grep CRON /var/log/syslog:

Jun 29 17:22:01 my_server CRON[21164]: (root) CMD (/bin/bash -l -c 'cd /var/www/my_app/current && RAILS_ENV=staging bundle exec rake cron_test')

Any ideas?

Thanks

Community
  • 1
  • 1
emstack
  • 33
  • 6

2 Answers2

1

What happens when you manually run the command from crontab on the console? Try to run in. See if there are any errors.

/bin/bash -l -c 'cd /var/www/my_app/current && RAILS_ENV=staging bundle exec rake cron_test'

Might be something with local or cron environment. Also logs would be great, capturing any running errors. You can set output log to capture any problems with crontab running the command.

  set :output, '/path/to/file.log'

for you example:

set :output, '/var/log/my_app.cron.log'
set :environment, "staging"
set :path, "/var/www/my_app/current"
every 2.minutes do
  # specify the task name as a string
  rake 'cron_test'
end

Here's the documentation whenever output redirection

Stan Brajewski
  • 452
  • 2
  • 5
1

Thanks it is working now!

When I created the /var/log/staging_cron.log I read this:

/bin/bash: bundle: command not found

I added env :PATH, ENV['PATH'] on the first line of my schedule.rb, updated the crontab (whenever --update-crontab) and now my test file is well created and updated.

emstack
  • 33
  • 6