Bash – Why is the trap not printing any log message

bashshell-scriptsignalstrap:

What I'm doing is,

trap 'rm -f /path/of/file/fileName.running; echo "TRAPPED & READY";' 1 2 9 15 >> trap.log

I didn't get anything in log & the file which should have been deleted still exists, I'm not sure of which signal could be stopping the script while it's running as I'm just killing the servers which are being used by the script.

NB: My Cron-Scheduled job won't run if that file is present & I don't think I've missed any possible signal.
where am I wrong?

Best Answer

Part of your problem is that you have the >> trap.log outside the (quoted) command arg, so all you’re getting in the trap.log file is the output from the trap command itself – which is nothing. I’m not sure what you mean by saying “TRAPPED & READY” when your script is terminating, but it looks like what you mean is

trap 'rm -f filename; echo "message" >> trap.log' sigspec

And I agree with Karlo: if you are “just killing the servers which are being used by the script,” then the script is quite probably exiting (rather than being killed by a signal) and you should use the EXIT (or, equivalently, 0) sigspec (possibly in addition to 1, 2, and 15).

P.S. You don’t need a semicolon (;) at the end of the trap command arg.

Related Question