bash: start multiple chained commands in background

前端 未结 15 919
生来不讨喜
生来不讨喜 2020-12-07 22:47

I\'m trying to run some commands in paralel, in background, using bash. Here\'s what I\'m trying to do:

forloop {
  //this part is actually written in perl
          


        
15条回答
  •  时光取名叫无心
    2020-12-07 23:25

    Another way is to use the following syntax:

    { command1; command2; command3; } &
    wait
    

    Note that the & goes at the end of the command group, not after each command. The semicolon after the final command is necessary, as are the space after the first bracket and before the final bracket. The wait at the end ensures that the parent process is not killed before the spawned child process (the command group) ends.

    You can also do fancy stuff like redirecting stderr and stdout:

    { command1; command2; command3; } 2>&2 1>&1 &
    

    Your example would look like:

    forloop() {
        { touch .file1.lock; cp bigfile1 /destination; rm .file1.lock; } &
    }
    # ... do some other concurrent stuff
    wait # wait for childs to end
    

提交回复
热议问题