Dmitry T.
Dmitry T.

Reputation: 506

Chef run sh script

I have a problem trying to run shell script via Chef (with docker-provisioning).

This is how I try to execute my script:

bash 'shell_try' do
   user "root"
   run = "#{some_path_to_script}/my_script.sh some_params"
   code " #{run} > stdout.txt 2> stderr.txt"
end

(note that this script should run another scripts, processes and write logs)

Here's no errors in the output, but when I log into machine and run ps aux process isn't running.

I guess something wrong with permissions (or env variables), because when I try the same command manually - it works.

Upvotes: 1

Views: 1012

Answers (3)

Dmitry T.
Dmitry T.

Reputation: 506

Finally find a solution (thanks to @coderanger) -

  1. Install supervisor:

    • Download supervisor cookbook
    • Add: include_recipe 'supervisor::default'
  2. Add my service to supervisor:

    supervisor_service "name" do action :enable #action :start command '/path/script.sh start'

    end

  3. Run supervisor service

  4. All done!

Upvotes: 1

Ken Brittain
Ken Brittain

Reputation: 2265

Please see the Chef documentation for your resource: https://docs.chef.io/resource_bash.html. The bash resource does not support a run attribute. Text of the code attribute is run as a bash script. The default action is to run the script unless told otherwise by the resource.

bash 'shell_try' do
  user "root"
  code " #{run} > stdout.txt 2> stderr.txt"
  action :run
end

The code attribute is written to a temporary file where it is then run using the attributes specified in the resource.

The line run = "#{some_path_to_script}/my_script.sh some_params" at this point does nothing.

Upvotes: 0

coderanger
coderanger

Reputation: 54267

A bash resource just runs the provided script text directly, if you wanted to run a long-running process generally you would set up an Upstart or systemd service and use the service resource to start it.

Upvotes: 1

Related Questions