Ask Your Question

How to schedule a Cron job task based on existence of file

asked 2018-11-29 01:46:09 -0600

z0r gravatar image

updated 2018-12-04 11:53:11 -0600

DarylW gravatar image

Hi all,

I'm looking for a way to create a Cron job task without knowing the executable location. I've written a function that looks for the executable in multiple possible paths but I don't know how to schedule the task based on that.

 function my_module::create_cron_job(  
 String $cmd,   String $args,   String
 $cwd,   String $cron_job_username,  
 String $cron_job_minutes ) {  
 $search_paths = [
     '/bin/', '/sbin/', '/usr/bin/', '/usr/sbin/', '/usr/local/bin',
     '/usr/local/sbin'   ] #-c ${package_dir}/config/${run_yaml}  
 $search_paths.each |String
 $search_path| {
     exec {"check_presence_${search_path}":
       command => '/bin/true',
       onlyif => "/usr/bin/test -e ${search_path}/${cmd}",
     }   }

and this is the cron structure:

   cron { "${cron_job_name}":
     command => "$(search_path)/$(cmd) $args &>/dev/null &",
     cwd     => $cwd,
     user    => $cron_job_username,
     minute  => "*/${cron_job_minutes}",
     hour    => "*",
     require => Exec("")   }

What i'm looking for is how to get the right $search_path to put in the cron structure.

Any help will be appreciated. Thanks.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2018-12-04 11:56:06 -0600

DarylW gravatar image

So your problem is that you want your cron set up with the exact path to the command you will execute - how about you create a script with the logic to find the path that you need in your script, and you just execute your script in the cron, and manage the script with puppet?

The other option, as mentioned above, is execute your 'script' as an external fact on the box, and use the path it returns to specify the path of the executable/. I never like that approach personally, but sometimes that's the way you have to 'work around' what you can do in puppets agent/master based model

edit flag offensive delete link more

answered 2018-12-03 08:50:37 -0600

mmars gravatar image

What if you create a fact that checks for the file?

Then you can use the fact in a conditional before creating the cron job.


if $::my_file {
 cron { "new_resource" : }
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2018-11-29 01:46:09 -0600

Seen: 194 times

Last updated: Dec 04