How to Save Hashicorp Nomad Logs with Nomad CLI and Bash

The aim of this page📝 is to explain how to modify a bash script to redirect output to a file and remove ANSI escape codes, based on the particular example of a script used to print HashiCorp Nomad logs. In my case, I constantly check the logs and often forward them, say as an attachment to Jira tickets.

Pavol Kutaj
3 min readSep 20, 2023
  • Originally, I had a bash script that printed HashiCorp Nomad logs.
#!/bin/bash
nomad_job_id_regex="(?<=jobs/)\S+"
job=$(echo "$1" | grep -Po "$nomad_job_id_regex")
echo
echo "-----STERR-----"
nomad alloc logs -job --stderr "$job"
echo
echo "-----STDOUT-----"
nomad alloc logs -job "$job"
  • I wanted to modify the script to redirect the output to two separate files, one for stderr and one for stdout.
  • The tee command can be used to write output to a file while also sending it to stdout.
  • However, my logs contained ANSI escape codes, which are used to colorize output.
│ 
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│ 
  • the escape codes in my logs are used to colorize the output. Here’s what they mean:
  • : This sets the text color to red.
  • : This resets all attributes (color, bold, etc.) to their defaults.
  • : This sets the text attribute to bold.
  • : This sets the text attribute to underline.
  • : This sets the text color to bright black (also known as gray).

So for example, ╷ would display a red "╷" symbol, and Error: Invalid index would display the word "Error:" in bold red, followed by "Invalid index" in bold with default color.

These codes are interpreted by my terminal or terminal emulator to display colored or styled text. When you redirect the output to a file or view it in a context that doesn’t interpret these codes (like a text editor), they appear as strange characters.

  • The modified script:
#!/bin/bash
nomad_job_id_regex="(?<=jobs/)\S+"
job=$(echo "$1" | grep -Po "$nomad_job_id_regex")
echo
echo "-----STERR-----"
nomad alloc logs -job --stderr "$job" | sed 's/\x1b\[[0-9;]*m//g' | tee temp_stderr.log
echo
echo "-----STDOUT-----"
nomad alloc logs -job "$job" | sed 's/\x1b\[[0-9;]*m//g' | tee temp_stdout.log
  1. #!/bin/bash: This is called a shebang. It tells the system that this script should be executed using the bash shell.
  2. nomad_job_id_regex="(?<=jobs/)\S+": This line defines a regular expression that matches any non-whitespace characters (\S+) that come after "jobs/" ((?<=jobs/)) in a string.
  3. job=$(echo "$1" | grep -Po "$nomad_job_id_regex"): This line uses the grep command with the -P option (which enables Perl-compatible regular expressions) and the -o option (which prints only the matching parts of a line) to find the job ID in the first argument to the script ($1). The result is stored in the variable job.
  4. echo: This command prints a blank line to stdout.
  5. echo "—————STERR—————": This command prints "—————STERR—————" to stdout.
  6. nomad alloc logs -job --stderr "$job" | sed 's/\x1b\[[0-9;]*m//g' | tee temp_stderr.log: This line does several things:
  • nomad alloc logs -job --stderr "$job": This command fetches the stderr logs for the Nomad job with the ID stored in job.
  • | sed 's/\x1b\[[0-9;]*m//g': The output from the previous command is piped (|) into sed, which removes all ANSI escape codes from it.
  • | tee temp_stderr.log: The output from the previous command is piped into, which writes it to a file named "temp_stderr.log" and also prints it to stdout.

The next three lines do essentially the same thing but for stdout instead of stderr. This script allows you to fetch and clean up Nomad logs, and write them to separate files for stderr and stdout. It’s a great example of how you can use bash scripting to automate tasks and make your life easier!

--

--

No responses yet