Hi, I have a problem with a trap handler in a script, which is doing some logging and needs external commands (date and hostname). In some cases there seems to be a race condition causing a syntax error every once in a while. I am assuming it is a race condition, because the syntax errors only happen very very infrequently.
I have produced the following script as a small example: ----------------------- #!/bin/bash log() { local text="$(date +'%Y-%m-%d %H:%M:%S') $(hostname -s) $1" echo $text >> /dev/null } thread() { while true; do log "Thread is running" kill -ALRM $$ sleep 1 done } trap "log 'received ALRM'" ALRM thread & trap "kill $?; exit 0" INT TERM while true; do log "Main is running" sleep 1 done ----------------------- Very infrequently this script will fail with a syntax error in line 5 (echo $text >> /dev/null). The actual error message is: > /path/to/script.sh: command substitution: line 5: syntax error near > unexpected token `)' > /path/to/script.sh: command substitution: line 5: `hostname -s) $1' Since there is not "hostname -s) $1" in line 5, I am assuming there also is an off-by-one error and line 4 is actually meant (local text="$(date +'%Y-%m-%d %H:%M:%S') $(hostname -s) $1"). I have encountered this problem both on bash 4.2.24(1)-release (x86_64-pc-linux-gnu) on ubuntu 12.04 as well as on bash 4.1.2(1)-release (x86_64-redhat-linux-gnu) on RHEL 6.2. There may be something wrong with the way traps are used in this case, but the documentation is very sparse on this topic. I also opened a question on StackOverflow.com (http://stackoverflow.com/questions/10194837/concurrent-logging-in-bash-scripts) but did not receive any usefull answers yet. Since this is a race condition, it might take a while for the bug to hit. In some cases the script was running up to 30 minutes before the bug triggered. Please let me know if you have any furhter questions or hints on how to resolve this issue. Thank you, Till Crueger