문제

I have a shell script which I want to invoke in background, from another shell script. Both the scripts are bash scripts.
First script(a.sh) is something like:

read a
echo 'something'
read b
echo 'something else'
# some if else conditions
nohup bash b.sh 2>&1 > /tmp/log &

When I try to execute the above script as: ./a.sh arg1 arg2 | tee log , very strangely it stucks at the nohup line for the second script b.sh to finish whereas it should not.

But when I just have the following line in the script, it works as expected:
nohup bash b.sh 2>&1 > /tmp/log &

Please help. I can also share the exact scripts if required.

도움이 되었습니까?

해결책

This is what you wanted to happen:

  1. The script runs and output things
  2. The script runs a command in the background
  3. The script exits
  4. The pipe closes
  5. tee exits

This is what's happening:

  1. The script runs and outputs things
  2. The script runs a command in the background, with the pipe as stderr
  3. The script exits
  4. The pipe is still held open by the backgrounded command
  5. tee never exits.

You fixed it by pointing stderr to the log file rather than the pipe, so that the backgrounded process no longer keeps the pipe open.

The meaning of the order of 2>&1 and the redirect has been explained in other answers, and shellcheck would have automatically pointed out this problem in your code.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top