The node exporter's textfile collector is handy for monitoring machine-level cronjobs. How would you go about that?

Let's say you had a simple bash script and you wanted to track how long it takes, and when it last ran. You could do something like:


# Adjust as needed.
# Note the start time of the script.
START="$(date +%s)"

# Your code goes here.
sleep 10

# Write out metrics to a temporary file.
END="$(date +%s)"
cat << EOF > "$TEXTFILE_COLLECTOR_DIR/myscript.prom.$$"
myscript_duration_seconds $(($END - $START))
myscript_last_run_seconds $END

# Rename the temporary file atomically.
# This avoids the node exporter seeing half a file.
mv "$TEXTFILE_COLLECTOR_DIR/myscript.prom.$$" \


Once you ensure that the flag on the node exporter is set to the right directory you're good to go. You could add any more metrics you think are useful, such as whether the script succeeded.

The node exporter exposes a metric called node_textfile_mtime_seconds which indicates when each textfile collector file was last modified, which can be useful for detecting if a cronjob hasn't run in a while.

Instead of using a temporary file, you could also use sponge from moreutils to handle that for you.


Wondering how best to monitor batch jobs? Contact us.