I used both the execute resource or the bash resource.
Both achieve the same result:
bash 'Execute my script' do user 'root' cwd '/mydir' code <<-EOH ./myscript.sh EOH end execute 'Execute my script' do user 'root' cwd '/mydir' command './myscript.sh' end The only difference I see is that bash actually creates a shell script (named /tmp/chef-script#{date}{#id}) where code is written.
What is the best practice to execute a shell script with Chef between execute or bash resource ?
2 Answers
For a single script, use an execute. The bash resource is for including the script contents inline in the recipe code.
In bash & execute block we need to write code to catch the error as if you add more than one command & the chef takes the status of the last command. To make it more clear - when the Bash/execute block has only one command chef catches the issue ,if the next command is successful then it takes the last command status.
bash 'Execute my script' do user 'root' cwd '/mydir' code <<-EOH ./myscript.sh ls srini ##This will fail ls ## this will be successful EOH end execute 'Execute my script' do user 'root' cwd '/mydir' command './myscript.sh' command 'ls srini' #will fail command 'ls' # will be successful end 1