1

I am using the following trap:

trap OnExit EXIT

And in my OnExit function I would like to capture all the info I can on what happened so I can write it to a log file. Id like to know who, why, how, when etc..

Is any of this possible?

Anthon
  • 78,313
  • 42
  • 165
  • 222
TCZ8
  • 1,069
  • 4
  • 14
  • 24

1 Answers1

1

1. who - as another user cannot send signals to your processes, it is most likely that it will be the process owner user (the root user still can, but I don't know a way to find out if root kills your process).

2. why - maybe something like this:

echo "Why did you interrupted the process?" && read why && echo $why > log

(I'm not sure what you mean under why).

3. how - as BASH receives the same signal if the user presses Ctrl + C or if he sends an interrupt via kill, AFAIK it is impossible to find out how exactly did the interrupt happen.

4. when - that's easy, use the date command:

date > log

EDIT: if you meant determining the signal type under "why" and "how", then see this Q&A on StackOverflow.

psimon
  • 1,212
  • 10
  • 26
  • 1
    I think either `why` or `how` was intended to mean "What signal caused the termination?" – Barmar Jun 25 '14 at 20:43
  • 1
    Yes, that might be. Edited answer. – psimon Jun 25 '14 at 20:50
  • Yes that is exactly it! I was hoping for some hidden variable i didn't know about but it will have to do. Thanks again for all your help. – TCZ8 Jun 25 '14 at 21:08
  • Which Q&A? Was there suposed to be a link? – TCZ8 Jul 02 '14 at 04:37
  • The word "this" is supposed to be clickable. Anyway, here it is: https://stackoverflow.com/questions/2175647/is-it-possible-to-detect-which-trap-signal-in-bash – psimon Jul 02 '14 at 08:39