2017-02-09 19:28:06 +00:00
---
title: About the crontab Resource
2018-02-16 00:28:15 +00:00
platform: linux
2017-02-09 19:28:06 +00:00
---
# crontab
2019-04-26 18:24:29 +00:00
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).
2017-02-09 19:28:06 +00:00
2017-10-03 21:35:10 +00:00
<br>
2018-08-09 12:34:49 +00:00
## Availability
### Installation
2019-04-26 18:24:29 +00:00
This resource is distributed along with Chef InSpec itself. You can use it automatically.
2018-08-09 12:34:49 +00:00
### Version
This resource first became available in v1.15.0 of InSpec.
2017-02-09 19:28:06 +00:00
## Syntax
A `crontab` resource block declares a user (which defaults to the current user, if not specified), and then the details to be tested, such as the schedule elements for each crontab entry or the commands itself:
describe crontab do
its('commands') { should include '/some/scheduled/task.sh' }
end
2019-03-21 15:49:00 +00:00
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.
2017-10-03 21:35:10 +00:00
<br>
2017-02-09 19:28:06 +00:00
2018-02-15 14:33:22 +00:00
## Examples
2017-02-09 19:28:06 +00:00
2019-04-26 18:24:29 +00:00
The following examples show how to use this Chef InSpec audit resource.
2017-02-09 19:28:06 +00:00
### Test that root's crontab has a particular command
describe crontab('root') do
its('commands') { should include '/path/to/some/script' }
end
### Test that myuser's crontab entry for command '/home/myuser/build.sh' runs every minute
describe crontab('myuser').commands('/home/myuser/build.sh') do
its('hours') { should cmp '*' }
its('minutes') { should cmp '*' }
end
### Test that the logged-in user's crontab has no tasks set to run on every hour and every minute
2018-06-06 18:10:48 +00:00
```ruby
describe crontab.where({'hour' => '*', 'minute' => '*'}) do
its('entries.length') { should cmp '0' }
end
```
2017-03-14 02:08:41 +00:00
2018-03-20 12:43:30 +00:00
### Test that the logged-in user's crontab contains a single command that matches a pattern
2017-03-14 02:08:41 +00:00
2018-06-06 18:10:48 +00:00
```ruby
describe crontab.where { command =~ /a partial command string/ } do
its('entries.length') { should cmp 1 }
end
```
2017-05-23 17:02:32 +00:00
2018-03-20 12:43:30 +00:00
### Test a special time string (i.e., @yearly /root/annual_report.sh)
2017-05-23 17:02:32 +00:00
2018-03-20 12:43:30 +00:00
describe crontab.commands('/root/annual_report.sh') do
2017-05-23 17:02:32 +00:00
its('hours') { should cmp '0' }
its('minutes') { should cmp '0' }
its('days') { should cmp '1' }
its('months') { should cmp '1' }
end
### Test @reboot case
describe crontab.commands('/root/reboot.sh') do
its('hours') { should cmp '-1' }
its('minutes') { should cmp '-1' }
end
2017-10-03 21:35:10 +00:00
<br>
2018-02-07 15:11:28 +00:00
## Property Examples
2018-02-26 16:11:06 +00:00
### Test a special time string
2018-02-07 15:11:28 +00:00
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
2019-04-26 18:24:29 +00:00
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.
2018-02-07 15:11:28 +00:00
<br>
2017-10-03 21:35:10 +00:00
## Matchers
2018-02-16 03:07:18 +00:00
For a full list of available matchers, please visit our [matchers page](https://www.inspec.io/docs/reference/matchers/).