0

I have a container, it starts with shell script.sh such as:

 FROM bash:4.4 COPY script.sh / COPY process.sh / CMD ["bash", "/script.sh"] 

Here is script.sh:

 #!/bin/sh sh process.sh & for i in {1..10000} do sleep 1 echo "Looping ... number $i" done 

It starts another process by running process.sh script.

Here is the process.sh script:

 #!/bin/sh for i in {1..10} do sleep 1 echo "I am from child process ... number $i" done 

Now I want to see all the stdout message. If I go to the directory like /var/lib/docker/containers/container_sha: I see something like below:

I am from child process ... number {1..10} Looping ... number 1 Looping ... number 2 Looping ... number 3 Looping ... number 4 Looping ... number 5 Looping ... number 6 ..... 

It is obvious that, I see only the script.sh output but not process.sh

Why is that? And how can i get all the logs?

Note: docker logs containerName does the same.

2
  • 1
    It is obvious that, I see only the script.sh output but not process.sh where do you think the line I am from child process ... number {1..10} comes from? Commented Mar 13, 2022 at 10:46
  • I found the issue with the syntax. Commented Mar 14, 2022 at 1:51

1 Answer 1

1

{1..10} is bash syntax, and does not expand to anything in sh. So the loop runs once, with the word {1..10} (literally).

You can run process.sh with bash instead of sh

Or if you want/need sh, you could either:

Use a counter:

while c=$((c+1)); [ "$c" -le 10 ]; do 

Use a program like seq (not POSIX):

for i in $(seq 10); do 

Iterate arguments passed from bash like:

sh process.sh {1..10} & 

and in process.sh:

for i do 
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your answer. If i ran ./process.sh & instead of sh process.sh &, it does the print as expected. I mean start printing the line such as I am from child process ... number 1 \n I am from child process ... number2 Why does it happen? I would appreciate your answer. Thanks
@OmarFaroqueAnik Backrounded processes are connected to the same tty and will print output at the same time as the foreground process, to the same tty (unless redirected). Make sure you use #!/bin/bash shebang in process.sh.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.