首页 > 解决方案 > 如何通过 Chef 运行 sh 脚本并显示 echo 输出

问题描述

我遇到了 Chef 和 sh 脚本的问题。我的 sh 脚本做了一些事情,对于每一步,它都会回显一条消息,表明某事是否成功完成。我想在 Chef 中运行此脚本,但它不起作用,我在日志中看不到任何内容,除了该脚本运行成功,这是不正确的。这是我的做法:

cookbook_file "/tmp/myLib.rpm" do
source "myLib.rpm"
mode '0775'
action :create
end

cookbook_file "/tmp/install.sh" do
    source "install.sh"
    mode '0755'
    action :create
end

execute "installing my lib" do
    command "sh /tmp/install.sh"
    user 'root'
    live_stream true
    action :run
end

该脚本已成功完成,但在日志中我只能看到:

Loading cookbooks [security@0.0.1]
INFO: *** Chef 12.18.31 ***
INFO: Storing updated cookbooks/security/recipes/install-mylib.rb in the cache.
INFO: Storing updated cookbooks/security/metadata.rb in the cache.
INFO: Storing updated cookbooks/security/files/default/install.sh in the cache.
INFO: Storing updated cookbooks/security/files/default/myLib.rpm in the cache.
INFO: Processing cookbook_file[/tmp/myLib.rpm] action create (security::install-mylib line 1)
INFO: Processing cookbook_file[/tmp/install.sh] action create (security::install-mylib line 7)
INFO: cookbook_file[/tmp/install.sh] created file /tmp/install.sh
INFO: cookbook_file[/tmp/install.sh] updated file contents /tmp/install.sh
INFO: cookbook_file[/tmp/install.sh] mode changed to 755
INFO: Processing execute[installing my lib] action run (security::install-mylib line 13)
INFO: execute[installing my lib] ran successfully

我看不到来自 install.sh 的任何回声,并且此脚本的工作尚未完成。当我直接在机器上运行这个脚本时,一切正常。

任何帮助或想法将不胜感激。

标签: bashchef-infraaws-opsworks

解决方案


运行脚本的更好选择是使用脚本资源。尽管execute资源也应该可以正常工作。

尝试使用script资源运行脚本,如下所示:

script 'installing my lib' do
  code '/tmp/install.sh'
  user 'root'
  interpreter 'bash'
  action :run
end

推荐阅读