美文网首页编程语言
Perl应用(1):通过shell脚本分割实现多任务投递

Perl应用(1):通过shell脚本分割实现多任务投递

作者: 魚晨光 | 来源:发表于2018-12-31 20:30 被阅读0次
    #!/usr/bin/perl -w
    
    use Getopt::Long;
    use File::Path;
    use File::Basename;
    use Cwd 'abs_path';
    my ($shell,$num,$shdir,$threads,$sign,$run);
    
    GetOptions(
      "i|in-file=s"  =>\$shell,
      "n|num=i" =>\$num,
      "s|sign=s" =>\$sign,  
      "o|outdir=s"  =>\$shdir,
      "r|run=s"  =>\$run,
      );
    
    
    ($shell && -s $shell)||die "Function: split shell script and qsub job 
    Usage : $0 -i <.sh> [-nstor]
    Version : v1 at 2016-12-20; 
    Contact: j_h_yu\@163.com
        -i  <str>   input shell script
        -n  <int>   the tasks num of each sub shell after splitting,default=1
        -s  <str>   set the sign split by,default='\\n\\n'
        -o  <str>   set outdir, default='./shellname.qsub.rundate'
        -r          if(y/Y) or not(n/N) run the scripts,default='y' 
    ";
    
    my $rundate=`date +'%m%d%H%M'`;
    chomp $rundate;
    
    $num    ||="1";
    $shdir  ||="./$shell.qsub.$rundate";
    $sign   ||="\n\n";
    $run    ||='y';
    
    $shdir=abs_path($shdir);
    $shdir|| `mkdir -p $shdir`;
    mkpath $shdir unless ( -d $shdir);
    
    $/=$sign;
    open IN,$shell or die "ERROR: open $shell: $!";
    my $name=basename($shell);
    $name=~s/\.sh$//;
    $name.=".sub";
    my $flag=1;
    my $n=0;
    my $qsub='';
    my %sh;
    open OUT, ">$shdir/$name.$flag.sh" || die "ERROR: write $name.$flag.sh: $!";
    $sh{"$name\.$flag\.sh"}=1;
    while(<IN>){
        chomp;
        $n++;
        if($n > $num){
            $n=1;
            $flag++;    
            close OUT;
            open OUT, ">$shdir/$name.$flag.sh" || die "ERROR: write $name.$flag.sh: $!"; 
            $sh{"$name\.$flag\.sh"}=1;      
        };
        print OUT "$_\n\n";
    }
    close IN;
    close OUT;
    $/="\n";
    for(sort (keys %sh)){$qsub.="& sh $_ &>$_\.log"};
    $qsub=~s/^\&//;
    system "cd $shdir;$qsub\n" if($run=~/y/i);
        
    

    相关文章

      网友评论

        本文标题:Perl应用(1):通过shell脚本分割实现多任务投递

        本文链接:https://www.haomeiwen.com/subject/jtxtcqtx.html