Not able to send a input.exec script to influxdb


I am tring to send via telegraf.conf a shell script result to influxdb but I am not able to do that.
This is the result of the script
$:/home/monitor# ./
{ “connections”: 2056, “gateway”: }

This is the telegraf.conf input:
commands = [ “/home/monitor/” ]
timeout = “5s”
name_override = “connect”
name_suffix = “”
data_format = “json”
tag_keys = [ “gateway” ]

This is the error running telegraf debug
[inputs.exec] Error in plugin: exec: fork/exec /home/monitor/ exec format error for command ‘/home/monitor/’:

I am new to this and sorry it would be simple to fix.


Your output from the gateway connections script is not valid JSON, the gateway is missing a value. I tried to further reproduce with the following: I used the following shell script:

echo '[ { "connections": 2056, "gateway": "hostname"} ]'

and this config:

  omit_hostname = true


  commands = ["/home/powersj/telegraf/"]
  name_override = "connect"
  data_format = "json"
  tag_keys = [ "gateway" ]

and got this output:

connect,gateway=hostname connections=2056 1648473758000000000

Hopefully that helps, but let us know if you are still running into issues.

1 Like


Tks for the answer … I got the same error using the model script.
If you can take a look …

"" 4L, 71C written
root@virt27:/etc/telegraf# ./
[ { "connections": 2056, "gateway": "hostname"} ]
root@virt27:/etc/telegraf# cat

echo '[ { "connections": 2056, "gateway": "hostname"} ]'

root@virt27:/etc/telegraf# telegraf -test -config-directory /etc/telegraf/telegraf.d/ -input-filter=exec -debug
2022-03-28T16:07:01Z I! Starting Telegraf 1.20.3
2022-03-28T16:07:01Z I! Using config file: /etc/telegraf/telegraf.conf
2022-03-28T16:07:01Z D! [agent] Initializing plugins
2022-03-28T16:07:01Z D! [agent] Starting service inputs
2022-03-28T16:07:01Z E! [inputs.exec] Error in plugin: exec: fork/exec /etc/telegraf/ exec format error for command '/etc/telegraf/': 
2022-03-28T16:07:01Z D! [agent] Stopping service inputs
2022-03-28T16:07:01Z D! [agent] Input channel closed
2022-03-28T16:07:01Z D! [agent] Stopped Successfully
2022-03-28T16:07:01Z E! [telegraf] Error running agent: input plugins recorded 1 errors

Here is the telegraf.conf

# Telegraf Configuration
  cluster = "production"
  interval = "10s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  precision = ""
  debug = false
  quiet = false
  logfile = ""
  hostname = "xxxx"
  omit_hostname = true
  urls = ["http://myinflux:8086"]
  database = "telegraf"
  retention_policy = ""
  write_consistency = "any"
  timeout = "5s"
  service_address = "tcp://:8094"
  percpu = true
  totalcpu = true
  collect_cpu_time = false
  ignore_fs = ["tmpfs", "devtmpfs", "devfs"]
  # no config
  # no config
  # no config
  # no config
  # no config
  # no config
  # no config
  # no config
  commands = [ "/etc/telegraf/" ]
  name_override = "connect"
  data_format = "json"
  tag_keys = [ "gateway" ]
  servers = ["IP1", "IP2"]
  network = "udp"
  record_type = "A"
  domains = ["sites"]
  port = 53
  timeout = 2

This is still with error and not able to send to influxdb.
Thanks once more for the support.

Per this page it sounds like it may be caused due to your shebang not being the first time. In your cat output, it does look like your first line is empty. You should move the #!/bin/bash up one line.

1 Like

With you support I managed to send the data to influxdb using the echo script.
It was very helpfull
The point is that my script is very complex but works locally, when tested with telegraf debug but when I check the influxdb the value of my metric is always 0.
I manage to send the data creating a .json file and using a cat command. Now I have the right data but the script runs just once.
I do not know if is there a way to ask telegraf.conf to run the script again after some time.

Here is the script


#output1=$(docker ps -q --filter "name=gateway-public")
#output2=$(docker exec $output1 netstat -tapn |wc -l)
while [ "$i" -ne 0 ]
output2=$(docker exec $(docker ps -q --filter "name=gateway-public") netstat -tapn |wc -l)
output3="{\"connections\": $output2, \"gateway\": \"hostname\"}"
echo $output3 > output.json
cat output.json
sleep 10

Here the telegraf.conf I am using now

  commands = [ "/etc/telegraf/", "cat /etc/telegraf/output.json" ]
  timeout = "5s"
  name_override = "connect"
  data_format = "json"
  tag_keys = [ "gateway" ]

I am using the nohup command to run the script and update the output.json but it is not the better way to do that.

Once more, thank you very much for all the support.

I am not a bash expert, does this bash script runs infinitely? What is the purpose of the sleep 10 line?
I didn’t quite understand the details of the bash script, but be aware of the difference between these two telegraf input plugins: