Search code examples
consul

json configuration of consul watch handler


I'd like to run a script in sudo mode as my consul watch handler, I can run it with command

consul watch -type key -key mykey sudo -u myaccount /scripts/myscript.sh

But I don't know how to define it in json configuration, I've tried below but it does not works

{
  "watches":[{
    "type":"key",
    "key":"mykey",
    "handler_type":"script",
    "args":["sh","-c","sudo","-u","myaccount","/scripts/myscript.sh"]
  }]
}

I am using consul 1.5.2, this is the error:

[ERR] agent: Failed to run watch handler '[sh -c sudo -u myaccount /scripts/myscript.sh]': exit status 1

Can anyone tell me what's wrong with my json configuration?


Solution

  • I moved the sh -c

    I got it to work with:

    "watches":[{
      "type":"key",
      "key":"mykey",
      "handler_type":"script",
      "args":["/bin/sudo","-u","consul","/bin/sh","-c","/home/testscript.sh"]
    }]
    

    The -c requires the script to be executable. Also you need the correct sudo privileges. You might even remove the sh -c altogether when the script is executable