1

I made this script to get data from the nodes of a cluster computer:

...
for machine in ${machines}
do
    echo "compressing data from: " ${machine}
    oarsh ${machine} "tar -zcf data_${machine}.tar.gz /tmp/data" &
done 

I ran the script and when I came back I got this:

compressing data from:  machine1
...
compressing data from:  machineN
me@chinqchint-30:~$ tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
...

And I did not get the prompt back.

I logged in using a second terminal and I can see that all the .tar.gz files are already created, but I am concerned because I did not get the control of the command line back. Can I simply terminate the script?

Martin Drozdik
  • 12,742
  • 22
  • 81
  • 146
  • Why are you running those processes in the background? – trojanfoe Mar 03 '14 at 08:26
  • 1
    What else do you expect after running the command in the background, i.e. specifying `&`. – devnull Mar 03 '14 at 08:27
  • I run it in the background so that the for cycle does not need to wait for it to finish. In order for each node to compress its data simultaneously. I do not have much experience with the command line. Am I doing something wrong? – Martin Drozdik Mar 03 '14 at 08:29

3 Answers3

5

You can add a double && between commands to force the second to wait for the first.

For example,

tar -zcf data_${machine}.tar.gz /tmp/data && echo "DONE"

Also, you probably shouldn't be running the tar command in the background (&)

philshem
  • 24,761
  • 8
  • 61
  • 127
  • 3
    You should use `;` to force the second command to wait for the first. `&&` is only for the situations when, in additions to that, you don't want the second command to run unless the first one finished successfully. – user4815162342 Mar 03 '14 at 08:30
2

You can use the command wait after your for loop to wait for all the backgrounded commands to finish.

Without it, you immediately got control of your terminal back, but the backgrounded processes overwrote the existing prompt. This is purely cosmetic, the shell is still accepting commands, and you can just press 'enter' to redraw the prompt.

that other guy
  • 116,971
  • 11
  • 170
  • 194
1

When you computer fan stops screaming at you

Sorry.... couldn't help it this time...

tar -zcf data_${machine}.tar.gz /tmp/data && echo "DONE"

I agree with philshem && is a great way to know bc the left side runs first

tar -zcf data_${machine}.tar.gz /tmp/data 

Only if and when successfully completed will the right side run

echo "DONE" # And or some other logic here
jasonleonhard
  • 12,047
  • 89
  • 66
  • Here are a few of my related answers: https://stackoverflow.com/a/66681463/1783588 and https://unix.stackexchange.com/a/639793/91905 – jasonleonhard Mar 17 '21 at 21:55