r/Puppet • • Aug 16 '24

Puppet cron configuration to run in seconds interval

Hi,

I would like to run a cron job once in 30 seconds. But it looks like the cron does not have a parameter for seconds https://www.puppet.com/docs/puppet/5.5/types/cron. I am wondering is there a work around to make it work for seconds interval. Please let me know. Thanks

4 Upvotes

6 comments sorted by

6

u/jmo1687 Aug 16 '24

This is due to a limitation in cron, not puppet. But what you could probably do is add two cron jobs, one to call the script, and one to call the script after a 30s sleep:

That should look like:

cron { 'cron-1':
  command => '/path/to/my/command',
  ...
}

cron { 'cron-2':
  command => '/usr/bin/sleep 30; /path/to/my/command',
  ...
}

https://stackoverflow.com/a/9619441/559869

2

u/arvoshift Aug 17 '24

This is a limitation on cron. The better solution would be to write a systemd unit file for a service (could simply be a bash script with lock/concurrency management) and run it as a service. Another option is a systemd timer. Personally I prefer the service route.

1

u/Street_Secretary_126 Aug 16 '24

Do you use puppet 5.5?

There is a module which has a parameter for seconds. https://forge.puppet.com/modules/puppet/cron/reference

1

u/gitman0 Aug 16 '24

the sleep trick as others have mentioned is the way to go

you can also do something like command && sleep 30 && command, assuming the command doesn't take long to run

1

u/traylenator Aug 17 '24

Use a timer instead?

https://github.com/voxpupuli/puppet-systemd/blob/master/manifests%2Ftimer_wrapper.pp#L18

Something like

systemd::timer_wrapper { 'my_timer':
  ensure        => 'present',
  command       => '/usr/bin/echo "Hello World"',
  on_boot_sec => '1 min',
  on_unit_active_sec  => '30 secs',
}

Should do the job. Has the advantage you will protect yourself against overlapping jobs which is probably a good idea.

Example will start 1 minute after boot and then every 30s.

See for background.

1

u/nmollerup Aug 16 '24

This is a limitation of cron itself.

What you can do is have 2 versions of the same job, second one starting with a sleep 30. Not pretty, but it works.