在 Puppet 配置中,我确实遇到了导致 Puppet 每次都运行的问题。它是使用周期性 cron 作业执行的无主配置。
exec { 'postgresql::server::database::action1':
command => "sudo -u postgres psql -d ${db_name} -a -f /opt/folderx/v2/createdb.sql",
path => '/usr/pgsql-9.5/bin:/usr/bin:/bin',
user => 'root',
onlyif => "test $(sudo -u postgres psql -d ${db_name} -c 'SELECT version();' ; echo $?) -ne 0",
}
问题是 onlyif 测试的正确语法是什么?在 bash shell 中我会得到:
bash: test: too many arguments
基本上我需要测试给定的 $dbame 中是否存在一些表和函数
最佳答案
postrgres psql -d ...
是否在屏幕上打印了一些东西?如果是,则其输出作为参数传递给 test
。要隐藏输出,您可以使用 ... >/dev/null
重定向它。
test $(someCommand; echo $?) -ne 0
过于复杂,只写!一些命令
。这样您可能也不必重定向输出。
onlyif => "! sudo -u postgres psql -d ${db_name} -c 'SELECT version();' > /dev/null",
关于bash 测试 cmd 失败,参数过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46896392/