Use the crontab Chef InSpec audit resource to test the crontab entries for a particular user on the system. It recognizes special time strings (@yearly, @weekly, etc).
This resource is distributed along with Chef InSpec itself. You can use it automatically.
This resource first became available in v1.15.0 of InSpec.
A crontab resource block declares a user (which defaults to the current user) and the details to be tested, such as the schedule elements for each crontab entry or the exact commands themselves:
describe crontab do
its('commands') { should include '/some/scheduled/task.sh' }
end
Note
include matcher in this context specifies the entire list of commands that the crontab should include and not a particular substring that should be included by a command. The include matcher always matches a complete command invocation, including options and arguments. The path to the system crontab can also be supplied via:
describe crontab(path: '/etc/cron.d/some_crontab') do
its('commands') { should include '/path/to/some/script' }
end
Note that only the path or the user (and not both) should be supplied as arguments to the resource.
The following examples show how to use this Chef InSpec audit resource.
describe crontab('root') do
its('commands') { should include '/path/to/some/script -option arg' }
end
describe crontab('myuser').commands('/home/myuser/build.sh') do
its('hours') { should cmp '*' }
its('minutes') { should cmp '*' }
end
describe crontab.where({'hour' => '*', 'minute' => '*'}) do
its('entries.length') { should cmp '0' }
end
describe crontab.where { command =~ /a partial command string/ } do
its('entries.length') { should cmp 1 }
end
describe crontab.commands('/root/annual_report.sh') do
its('hours') { should cmp '0' }
its('minutes') { should cmp '0' }
its('days') { should cmp '1' }
its('months') { should cmp '1' }
end
describe crontab.commands('/root/reboot.sh') do
its('hours') { should cmp '-1' }
its('minutes') { should cmp '-1' }
end
describe crontab do
its('minutes') { should cmp '0' }
its('hours') { should cmp '0' }
its('days') { should cmp '1' }
its('weekdays') { should cmp '1' }
its('user') { should include 'username'}
its('commands') { should include '/some/scheduled/task.sh' }
end
Chef InSpec will automatically interpret crontab-supported special time strings. For example, a crontab entry set to run @yearly can be tested as if the entry was manually configured to run on January 1, 12 AM.
For a full list of available matchers, please visit our matchers page.
© Chef Software, Inc.
Licensed under the Creative Commons Attribution 3.0 Unported License.
The Chef™ Mark and Chef Logo are either registered trademarks/service marks or trademarks/servicemarks of Chef, in the United States and other countries and are used with Chef Inc's permission.
We are not affiliated with, endorsed or sponsored by Chef Inc.
https://docs.chef.io/inspec/resources/crontab/