Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Send a metric to StatsD from bash
# Send a metric to statsd from bash
#
# Useful for:
# deploy scripts (http://codeascraft.etsy.com/2010/12/08/track-every-release/)
# init scripts
# sending metrics via crontab one-liners
# sprinkling in existing bash scripts.
#
# netcat options:
# -w timeout If a connection and stdin are idle for more than timeout seconds, then the connection is silently closed.
# -u Use UDP instead of the default option of TCP.
#
echo "deploys.test.myservice:1|c" | nc -w 1 -u graphite.example.com 8125
@renatoargh

This comment has been minimized.

Copy link

@renatoargh renatoargh commented Feb 24, 2016

Awesome! :)

@scottgeary

This comment has been minimized.

Copy link

@scottgeary scottgeary commented Jul 24, 2016

I sometimes found that nc -w 0 hung (couldn't understand out why), so started to pipe into socat as a drop-in replacement alternative:

echo "deploys.test.myservice:1|c" | socat -t 0 STDIN UDP:graphite.example.com:8125
@macalinao

This comment has been minimized.

Copy link

@macalinao macalinao commented May 3, 2019

very useful, thanks :)

@jesusvazquez

This comment has been minimized.

Copy link

@jesusvazquez jesusvazquez commented Oct 4, 2019

Another suggestion:

echo "deploys.test.myservice:1|c" | nc -w 1 -cu graphite.example.com 8125

Note the -c, from nc --help:

  -c, --close                close connection on EOF from stdin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment