Skip to content

Commit

Permalink
Make crontab command customizable (#677)
Browse files Browse the repository at this point in the history
* Make crontab command customizable

* Add --crontab-command to README and update Changelog
  • Loading branch information
p8 authored and benlangfeld committed Dec 14, 2016
1 parent c7675c8 commit 473bf66
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions bin/whenever
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down
12 changes: 7 additions & 5 deletions lib/whenever/command_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]}")
Expand Down Expand Up @@ -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?
Expand Down

0 comments on commit 473bf66

Please sign in to comment.