Ask Your Question

run a exec only after checking a folder

asked 2016-07-26 17:51:58 -0500

puppetamateur gravatar image

I have servers with certain folder which has a bat file I need to execute. But not all the servers have the folder and I donot need to run it in those servers where the file is absent. How can I check if a folder is present and if it is, execute the exec resource.

edit retag flag offensive close merge delete


I am not sure if it's windows but on *nix system you can use guards e.g. "onlyif".

rajeevsharma gravatar imagerajeevsharma ( 2016-07-27 07:42:31 -0500 )edit

Are you managing the folder and bat file with Puppet? Is this a one time only run, or does it need to run on a schedule? If one time only, does the bat file log output or create files/folders somewhere? What does it do?

UBPClaw gravatar imageUBPClaw ( 2016-07-27 10:33:56 -0500 )edit

I am using windows. And it doesn't create a folder, instead deletes the folder. its uninstallation batch file. so when we run it, it deletes the folder too. So i need to figure out a away to see if the folder is present and if it is, run the batch inside the folder.

puppetamateur gravatar imagepuppetamateur ( 2016-07-27 16:11:30 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2016-07-28 04:41:33 -0500

rajeevsharma gravatar image

Hi, I don't have the code but I can tell you a workaround to make this work on windows. If you looking for a check condition before running the bat script, better define a custom facter.

The custom facter will set a variable folderExist=true/false (this can be done using powershell command inside custom facter script). Once you deploy this facter on windows node this will set this facter variable which will be available for your further puppet runs. In puppet manifest you can do like following:

if $foldeExist == 'true'
exec { "execute batch script":
 command => "..."
 provider => windows

Hope this helps.

edit flag offensive delete link more


You don't even need a powershell command. using ruby's native File type will work as well, you just need to hardcode the path to the file in question.

DarylW gravatar imageDarylW ( 2016-07-28 09:18:21 -0500 )edit

answered 2016-07-27 16:11:15 -0500

DarylW gravatar image

if you are externally (from puppet) managing that folder and/or the bat file, you can use the unless/onlyif parameter on the exec to decide when to run it. I'm not sure how to do that in windows syntax, but on Linux you could do something like

exec{ $cmd:
  onlyif => "/bin/test -e ${path_to_folder_or_bat_file}",

You can probably find an equivalent command that can be run to tell if the specified file exists.

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

1 follower


Asked: 2016-07-26 17:51:58 -0500

Seen: 183 times

Last updated: Jul 28 '16