0
votes

I have a resource that looks as below:

execute 'run_command' do
  command "chown -R user:user * "
  not_if Dir["/home/user/dir1/*"].empty?
end

I want to make sure that the resource is not executed if /home/user/dir1 is empty

2
You won't really save all that much time; chown is pretty fast. Why does it matter? - Todd A. Jacobs
when the directory is empty, the command doesn't work and throws an error - meallhour
The error is what? - StephenKing
Regarding "chmod is fast", please read about idempotency - StephenKing
Does the error occur, if the directory does not exist? - StephenKing

2 Answers

0
votes

Try this:

not_if { Dir.entries("/home/user/dir1/").select {|entry| ::File.join("home","user","dir1",entry) and !(entry =='.' || entry == '..') }.empty? }
-2
votes

Just close standard error. For example:

execute 'chown a directory' do
  command 'chown -R user:group /path/to/directory 2>&-'
end