Do something based on leadership status
Switch branches/tags
Nothing to show
Clone or download
zeroXten Merge pull request #3 from gneill794/master
Proper ACL environment variable for consul.
Latest commit 5fb22ab Jun 12, 2018


Do something based on leadership status using



It's just a python script with no additional modules. Download it and stick it somewhere.

NOTE A consul agent is required to be installed where you run this script.


$ consul-do
Usage: consul-do KEY [DEBUG]

Useful for running cronjobs in HA mode.

Run something like this on two or more servers:

*    * * * * /usr/bin/consul-do JOB-1 && /path/to/job1
*/10 * * * * /usr/bin/consul-do JOB-2 && /path/to/job2

Only one of the servers will be elected leader and will therefore run the job. Should the leader fail, a follower will take over.

To enable debug mode, add anything as a second argument. E.g.

$ consul-do JOB-1 debug
Found nodename: test.internal
Found KV store
Found session: e32c055d-c6ed-e277-45ad-079ba218bb01
Found session node, we're the leader

If you require a consul ACL

$ CONSUL_HTTP_TOKEN="mytoken" consul-do ... 


$ vagrant up

Run the following on each respective server

$ consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -node=agent-one -bind=
$ consul agent -server -join -data-dir /tmp/consul -node=agent-two -bind=
$ consul agent -server -join -data-dir /tmp/consul -node=agent-three -bind=

Now you can run a command on each server

$ while true; do /vagrant/consul-do testing && date; sleep 10; done