0

On Ubuntu, I have a bash script that runs an application.

When that app seg faults, I get some info in std err, e.g.

/usr/local/foo/script.sh: line 21: 494382 Segmentation fault      (core dumped) appplication_name_and_arguments_here

Std err is being redirected to a file, which is then going into a log monitoring system.

What I'd like to do is augment the stderr content with more helpful info when this happens, e.g. some environment variables, and a timestamp.

How can I do this?

Thanks.

Nik
  • 2,718
  • 23
  • 34

1 Answers1

2

You could try putting an ERR trap in script.sh. Try putting this close to the top of it as a start:

set -o errtrace

trap err_trap ERR

function err_trap
{
    printf 'ERR trap:\n' >&2
    printf '    PATH=%s\n' "$PATH" >&2
    printf '    PWD=%s\n' "$PWD" >&2
    printf '    date: %s\n' "$(date)" >&2

    exit 1
}

That's very rough and ready, but hopefully you will be able to enhance it to do what you want.

pjh
  • 6,388
  • 2
  • 16
  • 17