{"id":13395131,"url":"https://github.com/javan/whenever","last_synced_at":"2025-05-13T18:05:23.784Z","repository":{"id":502591,"uuid":"129791","full_name":"javan/whenever","owner":"javan","description":"Cron jobs in Ruby","archived":false,"fork":false,"pushed_at":"2024-07-31T22:44:25.000Z","size":537,"stargazers_count":8857,"open_issues_count":98,"forks_count":725,"subscribers_count":113,"default_branch":"main","last_synced_at":"2025-05-06T17:14:54.270Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/javan.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2009-02-16T03:19:48.000Z","updated_at":"2025-05-05T04:31:06.000Z","dependencies_parsed_at":"2023-02-15T00:16:48.760Z","dependency_job_id":"fffdd769-c154-41cf-bde8-eacb9d0bb792","html_url":"https://github.com/javan/whenever","commit_stats":{"total_commits":422,"total_committers":114,"mean_commits":"3.7017543859649122","dds":0.561611374407583,"last_synced_commit":"0687268046eaede8f1369a70870e04d3c0c83897"},"previous_names":[],"tags_count":42,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javan%2Fwhenever","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javan%2Fwhenever/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javan%2Fwhenever/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javan%2Fwhenever/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/javan","download_url":"https://codeload.github.com/javan/whenever/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253234140,"owners_count":21875553,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T17:01:43.134Z","updated_at":"2025-05-13T18:05:23.742Z","avatar_url":"https://github.com/javan.png","language":"Ruby","readme":"Whenever is a Ruby gem that provides a clear syntax for writing and deploying cron jobs.\n\n### Installation\n\n```sh\n$ gem install whenever\n```\n\nOr with Bundler in your Gemfile.\n\n```ruby\ngem 'whenever', require: false\n```\n\n### Getting started\n\n```sh\n$ cd /apps/my-great-project\n$ bundle exec wheneverize .\n```\n\nThis will create an initial `config/schedule.rb` file for you (as long as the config folder is already present in your project).\n\n### The `whenever` command\n\nThe `whenever` command will simply show you your `schedule.rb` file converted to cron syntax. It does not read or write your crontab file.\n\n```sh\n$ cd /apps/my-great-project\n$ bundle exec whenever\n```\n\nTo write your crontab file for your jobs, execute this command:\n\n```sh\n$ whenever --update-crontab\n```\n\nOther commonly used options include:\n```sh\n$ whenever --user app # set a user as which to install the crontab\n$ whenever --load-file config/my_schedule.rb # set the schedule file\n$ whenever --crontab-command 'sudo crontab' # override the crontab command\n```\n\n\u003e Note: If you run the whenever --update-crontab without passing the --user attribute, cron will be generated by the current user. This mean tasks that needs other user permission will fail.\n\nYou can list installed cron jobs using `crontab -l`.\n\nRun `whenever --help` for a complete list of options for selecting the schedule to use, setting variables in the schedule, etc.\n\n### Example schedule.rb file\n\n```ruby\nevery 3.hours do # 1.minute 1.day 1.week 1.month 1.year is also supported\n  # the following tasks are run in parallel (not in sequence)\n  runner \"MyModel.some_process\"\n  rake \"my:rake:task\"\n  command \"/usr/bin/my_great_command\"\nend\n\nevery 1.day, at: '4:30 am' do\n  runner \"MyModel.task_to_run_at_four_thirty_in_the_morning\"\nend\n\nevery 1.day, at: ['4:30 am', '6:00 pm'] do\n  runner \"Mymodel.task_to_run_in_two_times_every_day\"\nend\n\nevery :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot\n  runner \"SomeModel.ladeeda\"\nend\n\nevery :sunday, at: '12pm' do # Use any day of the week or :weekend, :weekday\n  runner \"Task.do_something_great\"\nend\n\nevery '0 0 27-31 * *' do\n  command \"echo 'you can use raw cron syntax too'\"\nend\n\n# run this task only on servers with the :app role in Capistrano\n# see Capistrano roles section below\nevery :day, at: '12:20am', roles: [:app] do\n  rake \"app_server:task\"\nend\n```\n\n### Define your own job types\n\nWhenever ships with three pre-defined job types: command, runner, and rake. You can define your own with `job_type`.\n\nFor example:\n\n```ruby\njob_type :awesome, '/usr/local/bin/awesome :task :fun_level'\n\nevery 2.hours do\n  awesome \"party\", fun_level: \"extreme\"\nend\n```\n\nWould run `/usr/local/bin/awesome party extreme` every two hours. `:task` is always replaced with the first argument, and any additional `:whatevers` are replaced with the options passed in or by variables that have been defined with `set`.\n\nThe default job types that ship with Whenever are defined like so:\n\n```ruby\njob_type :command, \":task :output\"\njob_type :rake,    \"cd :path \u0026\u0026 :environment_variable=:environment bundle exec rake :task --silent :output\"\njob_type :runner,  \"cd :path \u0026\u0026 bin/rails runner -e :environment ':task' :output\"\njob_type :script,  \"cd :path \u0026\u0026 :environment_variable=:environment bundle exec script/:task :output\"\n```\n\nPre-Rails 3 apps and apps that don't use Bundler will redefine the `rake` and `runner` jobs respectively to function correctly.\n\nIf a `:path` is not set it will default to the directory in which `whenever` was executed. `:environment_variable` will default to 'RAILS_ENV'. `:environment` will default to 'production'. `:output` will be replaced with your output redirection settings which you can read more about here: \u003chttp://github.com/javan/whenever/wiki/Output-redirection-aka-logging-your-cron-jobs\u003e\n\nAll jobs are by default run with `bash -l -c 'command...'`. Among other things, this allows your cron jobs to play nice with RVM by loading the entire environment instead of cron's somewhat limited environment. Read more: \u003chttp://blog.scoutapp.com/articles/2010/09/07/rvm-and-cron-in-production\u003e\n\nYou can change this by setting your own `:job_template`.\n\n```ruby\nset :job_template, \"bash -l -c ':job'\"\n```\n\nOr set the job_template to nil to have your jobs execute normally.\n\n```ruby\nset :job_template, nil\n```\n\n### Parsing dates and times\n\nWhenever uses the [Chronic](https://github.com/mojombo/chronic) gem to parse the specified dates and times.\n\nYou can set your custom Chronic configuration if the defaults don't fit you.\n\nFor example, to assume a 24 hour clock instead of the default 12 hour clock:\n\n```ruby\nset :chronic_options, hours24: true\n\n# By default this would run the job every day at 3am\nevery 1.day, at: '3:00' do\n  runner \"MyModel.nightly_archive_job\"\nend\n```\n\nYou can see a list of all available options here: \u003chttps://github.com/mojombo/chronic/blob/master/lib/chronic/parser.rb\u003e\n\n### Customize email recipient with the `MAILTO` environment variable\n\nOutput from the jobs is sent to the email address configured in the `MAILTO` environment variable.\n\nThere are many ways to further configure the recipient.\n\nExample: A global configuration, overriding the environment's value:\n\n```ruby\nenv 'MAILTO', 'output_of_cron@example.com'\n\nevery 3.hours do\n  command \"/usr/bin/my_great_command\"\nend\n```\n\nExample: A `MAILTO` configured for all the jobs in an interval block:\n\n```ruby\nevery 3.hours, mailto: 'my_super_command@example.com'  do\n  command \"/usr/bin/my_super_command\"\nend\n```\n\nExample: A `MAILTO` configured for a single job:\n\n```ruby\nevery 3.hours do\n  command \"/usr/bin/my_super_command\", mailto: 'my_super_command_output@example.com'\nend\n```\n\n### Capistrano integration\n\nUse the built-in Capistrano recipe for easy crontab updates with deploys. For Capistrano V3, see the next section.\n\nIn your \"config/deploy.rb\" file:\n\n```ruby\nrequire \"whenever/capistrano\"\n```\n\nTake a look at the recipe for options you can set. \u003chttps://github.com/javan/whenever/blob/master/lib/whenever/capistrano/v2/recipes.rb\u003e\nFor example, if you're using bundler do this:\n\n```ruby\nset :whenever_command, \"bundle exec whenever\"\nrequire \"whenever/capistrano\"\n```\n\nIf you are using different environments (such as staging, production), then you may want to do this:\n\n```ruby\nset :whenever_environment, defer { stage }\nrequire \"whenever/capistrano\"\n```\n\nThe capistrano variable `:stage` should be the one holding your environment name. This will make the correct `:environment` available in your `schedule.rb`.\n\nIf both your environments are on the same server you'll want to namespace them, or they'll overwrite each other when you deploy:\n\n```ruby\nset :whenever_environment, defer { stage }\nset :whenever_identifier, defer { \"#{application}_#{stage}\" }\nrequire \"whenever/capistrano\"\n```\n\nIf you use a schedule at an alternative path, you may configure it like so:\n\n```ruby\nset :whenever_load_file, defer { \"#{release_path}/somewhere/else/schedule.rb\" }\nrequire \"whenever/capistrano\"\n```\n\n### Capistrano V3 Integration\n\nIn your \"Capfile\" file:\n\n```ruby\nrequire \"whenever/capistrano\"\n```\n\nTake a look at the [load:defaults task](https://github.com/javan/whenever/blob/master/lib/whenever/capistrano/v3/tasks/whenever.rake) (bottom of file) for options you can set. For example, to namespace the crontab entries by application and stage do this in your \"config/deploy.rb\" file:\n\n```ruby\nset :whenever_identifier, -\u003e{ \"#{fetch(:application)}_#{fetch(:stage)}\" }\n```\n\nThe Capistrano integration by default expects the `:application` variable to be set in order to scope jobs in the crontab.\n\n### Capistrano roles\n\nThe first thing to know about the new roles support is that it is entirely\noptional and backwards-compatible. If you don't need different jobs running on\ndifferent servers in your capistrano deployment, then you can safely stop reading\nnow and everything should just work the same way it always has.\n\nWhen you define a job in your schedule.rb file, by default it will be deployed to\nall servers in the whenever_roles list (which defaults to `[:db]`).\n\nHowever, if you want to restrict certain jobs to only run on subset of servers,\nyou can add a `roles: [...]` argument to their definitions. **Make sure to add\nthat role to the whenever_roles list in your deploy.rb.**\n\nWhen you run `cap deploy`, jobs with a :roles list specified will only be added to\nthe crontabs on servers with one or more of the roles in that list.\n\nJobs with no :roles argument will be deployed to all servers in the whenever_roles\nlist. This is to maintain backward compatibility with previous releases of whenever.\n\nSo, for example, with the default whenever_roles of `[:db]`, a job like this would be\ndeployed to all servers with the `:db` role:\n\n```ruby\nevery :day, at: '12:20am' do\n  rake 'foo:bar'\nend\n```\n\nIf we set whenever_roles to `[:db, :app]` in deploy.rb, and have the following\njobs in schedule.rb:\n\n```ruby\nevery :day, at: '1:37pm', roles: [:app] do\n  rake 'app:task' # will only be added to crontabs of :app servers\nend\n\nevery :hour, roles: [:db] do\n  rake 'db:task' # will only be added to crontabs of :db servers\nend\n\nevery :day, at: '12:02am' do\n  command \"run_this_everywhere\" # will be deployed to :db and :app servers\nend\n```\n\nHere are the basic rules:\n\n  1. If a server's role isn't listed in whenever_roles, it will *never* have jobs\n     added to its crontab.\n  1. If a server's role is listed in the whenever_roles, then it will have all\n     jobs added to its crontab that either list that role in their :roles arg or\n     that don't have a :roles arg.\n  1. If a job has a :roles arg but that role isn't in the whenever_roles list,\n     that job *will not* be deployed to any server.\n\n### RVM Integration\n\nIf your production environment uses RVM (Ruby Version Manager) you will run into a gotcha that causes your cron jobs to hang.  This is not directly related to Whenever, and can be tricky to debug.  Your .rvmrc files must be trusted or else the cron jobs will hang waiting for the file to be trusted.  A solution is to disable the prompt by adding this line to your user rvm file in `~/.rvmrc`\n\n`rvm_trust_rvmrcs_flag=1`\n\nThis tells rvm to trust all rvmrc files.\n\n### Heroku?\n\nNo. Heroku does not support cron, instead providing [Heroku Scheduler](https://devcenter.heroku.com/articles/scheduler). If you deploy to Heroku, you should use that rather than Whenever.\n\n### Testing\n\n[whenever-test](https://github.com/heartbits/whenever-test) is an extension to Whenever for testing a Whenever schedule.\n\n### Credit\n\nWhenever was created for use at Inkling (\u003chttp://inklingmarkets.com\u003e). Their take on it: \u003chttp://blog.inklingmarkets.com/2009/02/whenever-easy-way-to-do-cron-jobs-from.html\u003e\n\nThanks to all the contributors who have made it even better: \u003chttp://github.com/javan/whenever/contributors\u003e\n\n### Discussion / Feedback / Issues / Bugs\n\nFor general discussion and questions, please use the google group: \u003chttp://groups.google.com/group/whenever-gem\u003e\n\nIf you've found a genuine bug or issue, please use the Issues section on github: \u003chttp://github.com/javan/whenever/issues\u003e\n\nRyan Bates created a great Railscast about Whenever: \u003chttp://railscasts.com/episodes/164-cron-in-ruby\u003e\nIt's a little bit dated now, but remains a good introduction.\n\n----\n\n[![Build Status](https://secure.travis-ci.org/javan/whenever.svg)](http://travis-ci.org/javan/whenever)\n\n----\n\nCopyright \u0026copy; 2017 Javan Makhmali\n","funding_links":[],"categories":["Ruby","Scheduled/Recurrence Jobs","Scheduling","定时任务","Gems"],"sub_categories":["Omniauth","Scheduling"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjavan%2Fwhenever","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjavan%2Fwhenever","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjavan%2Fwhenever/lists"}