Skip to main content

Scheduled Cron Jobs with the Whenever Gem Discussion

General • Asked by Chris Oliver

Hey!

I've used the gem but know I understand it a bit more. Just one question. The flags -l and -c in "...bin/bash..." are crontab flags or bash flags?

Thanks a lot!

Those would be flags for bash, not cron itself. -l helps make sure your command for runs in a login shell so everything runs as expected and -c just says "hey we're passing the command through as an argument instead of running a script.


Could this be used in some way so that if a user were signed in to an app, and wanted to configure some sort of email notification to go out after a certain time after an event, or similarly set some sort of scheduled date/time to send out an email? I'm guessing having a cron to run every minute which would trigger a script to check for such a schedule or delayed send?


Hi, I have just created arask to be an effortless way to regularly run tasks in Rails. Just like cron. Much easier to set up though.

For anyone who wants to use Models in tasks, you should load your environment on your task to load them before using them on the code as in:

task example: :environment do
end

Hey,
I have implement schedule tasks to existing project using rails 4, when i rake tasks on development mode works and database updated, but when i rake tasks on production server it's work but database not updated.
Here is my code

// schedule.rb

every :hour do
  rake "profile_update:inactive"
end
//profile_update.rake

namespace :profile_update do
  desc "Update gps status profiles"
  task inactive: :environment do
    @profile_short = Heartrate.select('Date_Format(TIMEDIFF(now(), MAX(heartrates.start_time)), "%H") selisih_date, profiles.id, MAX(heartrates.start_time) start_time').joins(:profile).where('heartrates.start_time BETWEEN ? AND ?', DateTime.now.end_of_day-1, DateTime.now.end_of_day).where('profiles.gps_status = 1').group(:profile_id).order('profiles.id ASC')
      if @profile_short.present?
      @profile_short.each do |profile|
        if profile.selisih_date.to_i < 1
          cek_safety =  SafetyAlert.select('Date_Format(TIMEDIFF(now(), created_at), "%H") selisih_date_safety').where(profile_id: profile.id).where('created_at BETWEEN ? AND ?', profile.start_time, DateTime.now.end_of_day).last
          if cek_safety.present?
          else
            if profile.selisih_date.to_i >= 1
              Profile.where(id: profile.profile_id).update_all(gps_status: 0)
            end
          end
        else
          cek_safety =  SafetyAlert.select('Date_Format(TIMEDIFF(now(), created_at), "%H") selisih_date_safety').where(profile_id: profile.id).where('created_at BETWEEN ? AND ?', profile.start_time, DateTime.now.end_of_day).last
          if cek_safety.present?
            if cek_safety.selisih_date_safety.to_i >= 1
              Profile.where(id: profile.id).update_all(gps_status: 0)
            end
          else
            Profile.where(id: profile.id).update_all(gps_status: 0)
          end
        end
      end
    end
  end

end

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.