Skip to content

KevinZ的小窝

Menu
  • Home
  • Categories
Menu

使用perl脚本批量运行创建的shell脚本中的所有命令

Posted on 2023年 7月 13日2023年 7月 13日 by KevinZhou

parallel_run.pl 脚本代码

#!/usr/bin/perl -w

use strict;
use Getopt::Long;
use Cwd 'abs_path';
use threads;

my ($lines, $max) = (1, 100);
GetOptions(
    "l:i" => \$lines,
    "m:i" => \$max,
);

my $usage = qq{
Usage: perl $0 [options] in.sh

Options:
-l  # of lines of each job [$lines]
-m  # of jobs to run concurrently [$max]
};

die $usage unless(@ARGV);

my $dir = abs_path($ARGV[0]) . '.workdir';
`mkdir -p $dir`;

#read input file and split
my $line_count = 0;
my $job_count = '0001';
my $content = '';
my (@script, @running_script, @running_script_name);
while(<>){
    next if(/^\s*$/ || /^\s*#/);
    $line_count++;
    $content .= $_;
    if($line_count % $lines == 0){
        &output_to($content, "$dir/$job_count.sh");
        $job_count++;
        $content = '';
    }
}
&output_to($content, "$dir/$job_count.sh") if($content);

#run split scripts
for(my $i=0; $i<@script; $i++){
    &join_all if(@running_script >= $max);
    my $tmp =  async { return system("sh $script[$i] >$script[$i].o 2>$script[$i].e"); };
    push @running_script, $tmp;
    push @running_script_name, $script[$i];
}
&join_all if(@running_script);

## sub routines ##
sub output_to{
    my ($content, $dest) = @_;
    push @script, $dest;
    open OUT, ">$dest" or die $!;
    print OUT $content;
    close OUT;
}

sub join_all{
    system("echo -n 'start running @running_script_name at ';date +'%Y-%m-%d %H:%M:%S'");
    for(@running_script){ $_->join() }
    (@running_script, @running_script_name) = ();
    system("echo -n 'finish at ';date +'%Y-%m-%d %H:%M:%S'");
}

使用:

# bash
nohup ./parallel_run.pl example.sh>example.sh 2>&1 &
2023 年 7 月
一 二 三 四 五 六 日
 12
3456789
10111213141516
17181920212223
24252627282930
31  
« 5 月   9 月 »

俺家的猫~

胖达~

© 2026 KevinZ的小窝 |

粤ICP备2023017690号

|

粤公网安备 44010402003004号