【问题标题】:Several commands in one line for cronjob in hiera (Puppet) yamlhiera(Puppet)yaml中的cronjob在一行中的几个命令
【发布时间】:2021-03-03 15:11:12
【问题描述】:

我有 hieradata yaml 文件,其中包含很多选项和一些 cronjobs 我想再添加一个 cronjob,其中包含多个带引号的 bash 命令:

hbase_test:
      cron: '*/5 * * * *'
      user: 'root'
      command: '(date && echo "describe \'my_table\'; exit" | hbase shell) >> hbase_test.log 2>&1;'
      flock: true

但这不起作用 - puppet 无法加载此类 yaml。 我也尝试了下一个方法:

command: "(date && echo \"describe 'my_table'; exit\" | hbase shell) >> hbase_test.log 2>&1;"

Puppet 没问题,hbase 启动成功,但是失败:

describe my_table; exit
NameError: undefined local variable or method `my_table' for #<Object:0xf9f041c>

正确的命令必须是:

describe 'my_table'; exit

我失去了报价!而且我不知道如何以正确的方式逃脱它们。 我也试过了

command:>
  (date && echo "describe 'my_table'; exit" | hbase shell) >> hbase_test.log 2>&1;

但是 cron 没有启动。

【问题讨论】:

    标签: bash yaml puppet quotes hiera


    【解决方案1】:

    您的:&gt; 之间需要一个空格。这就是你想要的:

    command: >
      (date && echo "describe 'my_table'; exit" | hbase shell) >> hbase_test.log 2>&1;
    

    您可以看到 YAML 如何解析为 JSON here

    【讨论】:

    • 感谢您的回复!我检查了这个:使用&gt; 不起作用。使用 &gt;- 有效,但会丢失引号。我试过这个命令:(date &amp;&amp; echo "describe 'my_table'") &gt;&gt; hbase_test.log 2&gt;&amp;1; 并在日志中得到这样的消息:describe my_table。您有一些解决方法的想法吗?
    【解决方案2】:

    最后我尝试了很多不同的方法:

    带空格键 没有空格键 "" 作为外引号和 ' 作为内引号 '' 作为外部引号和 " 作为内部引号 还有一些 但是他们所有人都无法以正确的方式转义引号 - 我总是出错! 所以我找到的唯一一个解决方案:

    1. 使用您需要的所有命令创建 bash 文件
    2. 在一个命令中运行 bash 脚本

    Hiera 命令:

    command: 'sh my_bash_script >> hbase_test.log 2>&1 && rm my_bash_script'
    

    Bash 脚本:

    date
    echo "describe 'my_table'; exit" | hbase shell
    

    这按预期工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-18
      • 1970-01-01
      • 2016-01-05
      • 1970-01-01
      相关资源
      最近更新 更多