bash: start multiple chained commands in background

前端 未结 15 924
生来不讨喜
生来不讨喜 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:30

    I don't know why nobody replied with the proper solution:

    my @children;
    for (...) {
        ...
        my $child = fork;
        exec "touch .file1.lock; cp bigfile1 /destination; rm .file1.lock;" if $child == 0;
        push @children, $child;
    }
    # and if you want to wait for them to finish,
    waitpid($_) for @children;
    

    This causes Perl to spawn children to run each command, and allows you to wait for all the children to complete before proceeding.

    By the way,

    print `some command`
    

    and

    system "some command"
    

    output the same contents to stdout, but the first has a higher overhead, as Perl has to capture all of "some command"'s output

提交回复
热议问题