0

TL;DR: My Whenever Gem scheduled task does not run at all automatically but will manually.

I posted about this earlier in the week but got no responses, I'm attempting to implement the Whenever gem into my RoR 5 application. Once a year I want it to run a method in my 'User' model, but for testing purposes I have made it once every minute, like so:

Schedule.rb:

set :output, "/home/ubuntu/workspace/log/cron.log"
set :environment, 'development'
every 1.minute do
    runner "User.(methodhere)"
end

User.rb:

def self.(methodhere)
  User.all.each do |user|
    user.update(remaining_days: user.total_days)
  end
end

In multiple places I have read that sometimes cronjobs dont run properly in development mode, so I jumped through all of the hoops to put my application into production mode, and that did not help.

I then found that you can manually run these jobs in the command line, which I then tried to do using the command found doing:

whenever --update-cron

then

crontab -l

which showed

# Begin Whenever generated tasks for: 
* * * * * /bin/bash -l -c 'cd /home/ubuntu/workspace && bundle exec 
bin/rails runner -e development '\''User.new.(methodhere)'\'' >> 
/home/ubuntu/workspace/log/cron.log 2>&1'

Running this manually:

/bin/bash -l -c 'cd /home/ubuntu/workspace && bundle exec 
bin/rails runner -e development '\''User.(methodhere)'\'' >> 
/home/ubuntu/workspace/log/cron.log 2>&1'

makes it work, and executes the (methodhere). Whenever just does not make it run automatically at the set interval.

Another thing I found was to try and restart cron, through cron restart, but I am receiving:

cron: can't open or create /var/run/crond.pid: Permission denied

I'm not sure if that has anything to do with the IDE I'm using, Cloud 9.

Many google searches have left me with nothing.

NOTE: I'm a very new developer with RoR, so any guidance on the matter would be greatly appreciated, thanks!

Ant-IWH
  • 59
  • 7
  • can you show us what's in `/home/ubuntu/workspace/log/cron.log`? – Anthony Apr 13 '18 at 14:49
  • @Anthony my cron.log is empty – Ant-IWH Apr 13 '18 at 14:58
  • What happens when you run this **in the same environment as cron**, rather than as a logged in user like you are currently? https://stackoverflow.com/a/2546509/1954610 – Tom Lord Apr 13 '18 at 15:04
  • You may also wish to (temporarily) not use any output redirection in order to see what's going on. – Tom Lord Apr 13 '18 at 15:06
  • I changed the environment to ~/cronenv as said in that submission, the new code produced when checking via 'whenever' in the terminal I will add to the bottom of the post. It seems to still not be running. I'm also not sure how to run 'env - `cat ~/cronenv` /bin/sh' properly. @TomLord – Ant-IWH Apr 13 '18 at 15:13
  • What do you mean, "not sure how to run it properly"? You literally copy+paste that command into the terminal. – Tom Lord Apr 13 '18 at 15:35
  • My mistake, I have never used cron before this so I wasn't sure if it should be run in the terminal or in the code. I ran that in the terminal and it seems to still not be updating. I'm considering finding another gem at this point. @TomLord – Ant-IWH Apr 13 '18 at 15:48
  • @Ant-IWH Back to my original question: **What happens** when you run the command in the same environment as cron? "It still seems to not be updating"... So what *does* it do? What output do you see? Is there an error? What is the error? – Tom Lord Apr 13 '18 at 16:13
  • Your "update after attempting my suggestion" is not really what I was looking for; I wanted to know what happens after you run `env - \`cat ~/cronenv\` /bin/bash -l -c 'cd /home/ubuntu/workspace && bundle exec bin/rails runner -e development '\''User.new.y(methodhere)'\''` in the terminal, not what happens when you re-generate the cron file via `whenever`. – Tom Lord Apr 13 '18 at 16:15
  • Possible duplicate of: https://stackoverflow.com/questions/49762726/ruby-on-rails-whenever-gem-schedule-rb-not-running-code-works-manually – spickermann Apr 13 '18 at 16:16
  • Apologies @TomLord, this is what it outputs when I run env - 'cat ~/cronenv' in the terminal: https://pastebin.com/SpBWsqBK Running env - 'cat ~/cronenv' /bin/bash -l -c 'cd /home/ubuntu/workspace && bundle exec bin/rails runner -e development '\''User.(methodhere)'\''' gives me: env: cat ~/cronenv: No such file or directory – Ant-IWH Apr 13 '18 at 16:24
  • @Ant-IWH I feel like we're going round in circles here... `~/cronenv` should exist, because you should have just created it in the previous step. Did you follow the instruction to generate that file, in the link I posted twice? https://stackoverflow.com/questions/2135478/how-to-simulate-the-environment-cron-executes-a-script-with/2546509#2546509 You can just add `* * * * * env > ~/cronenv` to your `crontab`, wait one minute, then the file will exist. – Tom Lord Apr 13 '18 at 16:43
  • What we're trying to do here, in case it's not obvious is: (1) Capture the exact environment that `cron` is running in, then (2) re-run the command that you say works, to see why it fails in `cron`'s environment. – Tom Lord Apr 13 '18 at 16:46
  • ...Because again, the "Tom's suggestion" in your edit is **not** what I was ever suggesting. I think you misunderstood. – Tom Lord Apr 13 '18 at 16:47
  • Maybe the issue is that your user is unable to create cron jobs - since the file permissions are set up wrong? In this case, the problem has got absolutely nothing to do with ruby/whenever. Maybe https://www.douban.com/note/624662347/ or https://serverfault.com/questions/274434/cron-permission-denied-on-everything will help? – Tom Lord Apr 13 '18 at 16:51
  • I believe it would be easier if I just private messaged you to explain better, would you mind if I did that? @TomLord – Ant-IWH Apr 13 '18 at 17:02

0 Answers0