Config Start / Run Time

I am building an inputs.exec config file that is executing a python script with an interval every 24h. Is there a way to know when it is going to start / first run it? I.e. It seems that the first run of that config file could be anytime in the interval window, not just when Telegraf is first started.

The AI response I found is that first run is whatever the interval time is from startup. That would make sense if so, so that you don’t have every single config file firing at the same time right at startup.

Based on my logs history I do not think this answer was correct, it definitely started sooner than 24 hours after Telegraf startup.

These MAY help:

  ## Default data collection interval for all inputs
  interval = "24h"

  ## Rounds collection interval to 'interval'
  ## ie, if interval="10s" then always collect on :00, :10, :20, etc.
  round_interval = true

  ## Collection jitter is used to jitter the collection by a random amount.
  ## Each plugin will sleep for a random time within jitter before collecting.
  ## This can be used to avoid many plugins querying things like sysfs at the
  ## same time, which can have a measurable effect on the system.
  collection_jitter = "0s"

  ## Collection offset is used to shift the collection by the given amount.
  ## This can be be used to avoid many plugins querying constraint devices
  ## at the same time by manually scheduling them in time.
  collection_offset = "0s"

But I don’t think this will solve your problem…

The above will ensure that the script executes “on-the-hour”
(change collection_offset to e.g. “15m” to make it run “a quarter past”)

It will not ensure that it is executed at a specific time, but change the script/write a wrapper that ensures that (remember to consider DST :slight_smile: )
This script should return a “fake” measurement, as telegraf wants something…:

fake,no=notnow fakeval=1

I read through all of those on GitHub too but none answer the question as to when the first trigger for a config file is going to happen after Telegraf is started.

You want to run something on a daily basis, but day isn’t a telegraf unit AFAIK, hence my suggestion to wrap it in a script to do that part of the check.

[[inputs.exec]]
  commands = [ "command" ]
  interval = "1h"
  round_interval = true
  collection_jitter = "0s"
  collection_offset = "15m"

SHOULD ensure to run command at 00:15:00 00, 01:15:00, 02:15:00, …, 23:15:00.

In this case, your command could check if the hour is, say 04 and emit dummy data otherwise.

You may also use a standard cronjob to create a logfile and then use inputs.tail to parse it

I am already using inputs.exec to run the script, so I definitely see your point about changing it to do a check for time and if not the time I want it to run, to emit fake data. That’s possible but like you said in the second part, at that point I could just use cron to schedule the file I already have. This was more honestly for just my own knowledge of the tool. The time of day doesn’t matter for when it starts, I just wanted to know if I was able to calculate when it was going to be.

@npm_engineer Hmmm it looks like it’s run here:

At the start of each interval, Telegraf invokes the Gather method of each input plugin, where it then expands glob patterns in the commands field using filepath.Glob

So for example if you have

[[inputs.exec]]
  commands = ["./myscript.sh"]
  timeout = "5s"
  interval = "30s"

Every 30 seconds, the exec plugin calls the Gather method. Then the script is executed with a timeout of 5 seconds.

Does that help?

Kind of… When does the exec plugin call the Gather method for the first time, immediate at startup? If so then the first poll should be whatever your interval is after startup.