Bash commands with redirected output don’t finish

I have a very simple bash script with a few commands executed in series. Each command’s output is redirected to its own log file. Something like this

(command --param a > sys.a.log 2>&1)
(command --param b > sys.b.log 2>&1)
(command --param c > sys.c.log 2>&1)

Now, when I run this script, it starts perfectly, but when the first command finishes (the log file shows the last line of the program has executed) the process doesn’t terminate. I have to kill it manually before the script moves on to the next line (and creates sys.b.log).

Is this how you would expect this script to behave (I don’t know much about bash), or is this unexpected behavior, and should I look elsewhere for the problem. If it matters, the actual command is “java”, with a bunch of parameters.

Answer

Its a good idea to use exit traps in your bash scripts

Some snippets: http://redsymbol.net/articles/bash-exit-traps/

Also, check out this post on writing robust shell scripts and things to look out for: http://www.davidpashley.com/articles/writing-robust-shell-scripts/

Attribution
Source : Link , Question Author : Peter , Answer Author : Dhruvan Ganesh

Leave a Comment