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.

  • The shell syntax is fine. The parens (subshells) may not be needed but they certainly shouldn't cause a hang. The problem is likely with your java command. – John1024 Aug 10 '16 at 22:00
  • @John1024 Without the parentheses, each log file captures the output of all commands before it, that's why I added them. When I try the java command in the terminal by itself it always terminates. – Peter Aug 10 '16 at 22:20
  • 1
    @John1024 Nope, never mind. I just tried it again, and it's the java program. (Seems to be some multithreading bug.) Thanks for the help. – Peter Aug 10 '16 at 22:25
  • @Peter Without the parentheses, each log file will only capture that one command; if it's capturing more than that, there's something else going on that isn't in your script excerpt. – Gordon Davisson Aug 12 '16 at 1:46
  • @Gordon, that's good to know. It is possible that something went wrong with the command itself and I misinterpreted it as a problem with the script. – Peter Aug 12 '16 at 9:13

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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.