Permalink
Browse files

Make crontab command customizable (#677)

* Make crontab command customizable

* Add --crontab-command to README and update Changelog
  • Loading branch information...
1 parent c7675c8 commit 473bf66c313dcc6e0e7d0378c246d5e05077619a @p8 p8 committed with benlangfeld Dec 14, 2016
Showing with 20 additions and 6 deletions.
  1. +2 −0 CHANGELOG.md
  2. +8 −1 README.md
  3. +3 −0 bin/whenever
  4. +7 −5 lib/whenever/command_line.rb
View
@@ -2,6 +2,8 @@
* Modify wheneverize to allow for the creating of 'config' directory when not present
+* Add --crontab-command to whenever binary for overriding the crontab command. [Martin Grandrath]
+
### 0.9.7 / June 14, 2016
* Restore compatibility with Capistrano v3; it has a bug which we have to work around [Ben Langfeld, Chris Gunther, Shohei Yamasaki]
View
@@ -34,9 +34,16 @@ This will simply show you your `schedule.rb` file converted to cron syntax. It d
$ whenever --update-crontab
```
+Other commonly used options include:
+```sh
+$ whenever --user app # set a user as which to install the crontab
+$ whenever --load-file config/my_schedule.rb # set the schedule file
+$ whenever --crontab-command 'sudo crontab` # override the crontab command
+```
+
You can list installed cron jobs using `crontab -l`.
-Run `whenever --help` for a complete list of options for selecting the schedule to use, setting variables in the schedule, selecting a user as which to install the crontab, etc.
+Run `whenever --help` for a complete list of options for selecting the schedule to use, setting variables in the schedule, etc.
### Example schedule.rb file
View
@@ -35,6 +35,9 @@ OptionParser.new do |opts|
opts.on('-r', '--roles [role1,role2]', 'Comma-separated list of server roles to generate cron jobs for') do |roles|
options[:roles] = roles.split(',').map(&:to_sym) if roles
end
+ opts.on('-x', '--crontab-command [command]', 'Default: crontab') do |crontab_command|
+ options[:crontab_command] = crontab_command if crontab_command
+ end
opts.on('-v', '--version') { puts "Whenever v#{Whenever::VERSION}"; exit(0) }
end.parse!
@@ -9,9 +9,10 @@ def self.execute(options={})
def initialize(options={})
@options = options
- @options[:file] ||= 'config/schedule.rb'
- @options[:cut] ||= 0
- @options[:identifier] ||= default_identifier
+ @options[:crontab_command] ||= 'crontab'
+ @options[:file] ||= 'config/schedule.rb'
+ @options[:cut] ||= 0
+ @options[:identifier] ||= default_identifier
if !File.exist?(@options[:file]) && @options[:clear].nil?
warn("[fail] Can't find file: #{@options[:file]}")
@@ -57,15 +58,16 @@ def whenever_cron
def read_crontab
return @current_crontab if @current_crontab
- command = ['crontab -l']
+ command = [@options[:crontab_command]]
+ command << '-l'
command << "-u #{@options[:user]}" if @options[:user]
command_results = %x[#{command.join(' ')} 2> /dev/null]
@current_crontab = $?.exitstatus.zero? ? prepare(command_results) : ''
end
def write_crontab(contents)
- command = ['crontab']
+ command = [@options[:crontab_command]]
command << "-u #{@options[:user]}" if @options[:user]
# Solaris/SmartOS cron does not support the - option to read from stdin.
command << "-" unless OS.solaris?

0 comments on commit 473bf66

Please sign in to comment.