cron supports using some special strings replacing the first five time matching fields:
Instead of the first five fields, one of eight special strings may appear:
string meaning
------ -------
@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".
The @reboot one is special and can't be emulated with the 5 usual fields thus it would be nice if there was a way for cron.present to use it.
Thanks for the heads up, I thought we had that one in there!
should not be hard to add since the scaffolding is already there
Not all cron daemons support this, so it would be prudent to do some OS detection with grains before adding this to the crontab.
Is there a reasonable way to check the installed cron package, instead of the OS? It's pretty easy to swap out cron daemons, and Arch users especially have been known to do it.
Good point. It should be easy enough to come up with a list of package names for cron daemons which support it, and run a check against that list.
Looks like this has been implemented
@sjorge Do you happen to have an example snippet on how to use @reboot? I'd appreciate it.
@didip I only have one location where I use it:
network.route::lx-helper:
file.directory:
- name: /etc/inet
cron.present:
- name: /native/usr/bin/awk '!/^($|[:space:]*#)/{ print "/native/usr/sbin/route add "$0 }' /etc/inet/static_routes | sh
- special: '@reboot'
But it's a very specific usage, in my case due to some lacking tooling support when running Ubuntu/CentOS under SmartOS using lxbranded zones... the routes are missing after a reboot. I inject them using a cron that fires on @reboot.
Thanks! That special keyword is all I need.
Was this ever implemented? I get the following error when trying to use special:
Warnings: 'special' is an invalid keyword argument for 'cron.present'. If
you were trying to pass additional data to be used in a template
context, please populate 'context' with 'key: value' pairs. Your
approach will work until Salt Carbon is out. Please update your
state files.
@grobinson-blockchain it seems to work fine (on linux) under 2016.3, not sure about earlier.
Wow! I just spent a long time fighting this. It turns out - special: @reboot which was wrong. In some incantations of this line, I was able to produce an error, but most just created the cron entry with no comment identifier and no "@" at the beginning of the line and re-executions created another similar line.
The only incantation that works is when @reboot is inside of quotes. This probably /shouldn't/ require quotes, but maybe using @ means it's needed because of yaml. It would be nice if - special: reboot added the "@" and anything else that won't work just generated an error.
From Wikipedia: "Two additional sigil characters are reserved in YAML for possible future standardisation: the at sign (@) and accent grave (`)."
So yes, the @ sign is being stripped. I'm not sure that some code to recognize strings that should have @ signs is needed, that sounds clunky. I think a documentation change to remind the user to use quotes would be sufficient.
Thank you all for the discussion here. Since the special option has been implemented and @lordcirth has now updated the documentation, I'll close this.
Most helpful comment
From Wikipedia: "Two additional sigil characters are reserved in YAML for possible future standardisation: the at sign (@) and accent grave (`)."
So yes, the @ sign is being stripped. I'm not sure that some code to recognize strings that should have @ signs is needed, that sounds clunky. I think a documentation change to remind the user to use quotes would be sufficient.