Monit の exec アクションで複数のコマンドを実行する方法

結論

シェルスクリプトなどを作ってそれを実行する。

だめな例

  • exec "/usr/bin/touch /tmp/foobar && /usr/bin/curl -I 'https://www.google.co.jp'"
  • exec "/usr/bin/touch /tmp/foobar & /usr/bin/curl -I 'https://www.google.co.jp'"
  • exec "/usr/bin/touch /tmp/foobar ; /usr/bin/curl -I 'https://www.google.co.jp'"

OKだけどつらい例

  • exec "/bin/bash -c '/etc/init.d/server restart && perl /home/sweet/crazy-stuff.pl'"
  • クォートのエスケープが混ざるとえらいことになる

OKな例

  • exec "/path/to/watashi_no_script.sh"

  • watashi_no_script.sh は以下のとおり

#!/bin/bash

touch /tmp/foo
touch /tmp/bar
touch /tmp/foobar

再度結論

したがって、シェルスクリプトなどの軽量スクリプトを作り、それを一文で実行するのがよい。

参考

however best practice recommends using a single script for all your actions ( meaning include the init.d restart command into your shell script.

Powered by はてなブログ