-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathrun_cmd_in_batch.pl
executable file
·147 lines (132 loc) · 3.75 KB
/
run_cmd_in_batch.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env perl
# 20211130: Fix grammar format
use strict;
use warnings;
BEGIN {
use lib "/usr/local/share/perl5/";
use lib "/usr/local/lib/perl5/site_perl/5.24.2/";
}
use LogInforSunhh;
use Parallel::ForkManager;
use Getopt::Long;
my %opts;
GetOptions(\%opts,
"help!",
"cpuN:i", "beginLn:i", "endLn:i", "nprocF:s",
"grpLn:i",
"wait_sec:i",
"rmComment!",
);
sub usage {
print STDOUT <<HH;
################################################################################
# perl $0 input_cmd_list
#
# -beginLn [0]
# -endLn [0]
# -cpuN [1]
# -nprocF [Nproc]
#
# -grpLn [1] Do be CAREFUL when using this!!!
#
# -rmComment [Boolean]
# -wait_sec [0]
################################################################################
HH
exit 1;
}
$opts{'cpuN'} //= 0;
$opts{'beginLn'} //= 0;
$opts{'endLn'} //= 0;
$opts{'nprocF'} //= 'Nproc';
$opts{'grpLn'} //= 1;
$opts{'grpLn'} >= 1 or &stopErr("[Err] -grpLn cannot be smaller than 1.\n");
$opts{'wait_sec'} //= 0;
-t and !@ARGV and &usage();
defined $opts{'help'} and &usage();
my @joblist;
while (<>) {
chomp;
push(@joblist, $_);
}
@joblist = &linearize(\@joblist);
$opts{'rmComment'} and @joblist = &remove_comment(\@joblist);
my $ttlN = scalar(@joblist);
&tsmsg("[Rec] Total $ttlN lines.\n");
$opts{'beginLn'} <= 0 and $opts{'beginLn'} = 1;
$opts{'endLn'} > $ttlN and $opts{'endLn'} = $ttlN;
$opts{'endLn'} <= 0 and $opts{'endLn'} = $ttlN;
&tsmsg("[Rec] Begin/End line modified to [$opts{'beginLn'} , $opts{'endLn'}]\n");
my $MAX_PROCESSES = $opts{'cpuN'} ; # Sometimes $parm{'cpuN'} - 1 may be better.
my $pm = new Parallel::ForkManager($MAX_PROCESSES);
for (my $i=$opts{'beginLn'}-1; $i<$opts{'endLn'}; $i+=$opts{'grpLn'}) {
$MAX_PROCESSES = &change_procN($pm, $opts{'nprocF'}, $MAX_PROCESSES);
if ($opts{'wait_sec'} > 0) {
sleep($opts{'wait_sec'});
}
my $pid = $pm->start and next;
my $i1 = $i+1;
for (my $j=0; $j<$opts{'grpLn'}; $j++) {
my $k = $i+$j;
$k < $opts{'endLn'} or last;
my $cmdLn = $joblist[$k];
if ( $cmdLn =~ m!^\s*$! ) {
&tsmsg("[Wrn] Skip [$i1+$j] empty command : $cmdLn\n");
} elsif ( $cmdLn =~ m!^\s*\#! ) {
&tsmsg("[Wrn] Skip [$i1+$j] commented command : $cmdLn\n");
} else {
&tsmsg("[Msg] Running [$i1+$j] command : $cmdLn\n");
&exeCmd_1cmd($cmdLn);
}
}
$pm->finish;
$MAX_PROCESSES = &change_procN($pm, $opts{'nprocF'}, $MAX_PROCESSES);
}
$pm->wait_all_children;
&tsmsg("[Rec] All commands over.\n");
################################################################################
# Sub-routines
################################################################################
sub change_procN {
my ($pm, $nprocF, $prev_maxP) = @_;
-e $nprocF or return $prev_maxP;
open F,'<',"$nprocF" or &stopErr("[Err] Failed to open [$nprocF].\n");
my $new_maxP = <F>;
chomp($new_maxP);
$new_maxP = (split(/\s+/, $new_maxP))[0];
$new_maxP = int($new_maxP);
close F;
if ($new_maxP > 0 and $new_maxP != $prev_maxP) {
$pm->set_max_procs($new_maxP);
&tsmsg("[Rec] Changing MAX_PROCESSES from $prev_maxP to $new_maxP\n");
}
return $new_maxP;
}# change_procN()
sub linearize {
my ($in_aref) = @_;
my @back;
my $is_cont = 0;
for my $a1 (@$in_aref) {
if ($is_cont == 1) {
$back[-1] .= "$a1";
$is_cont = 0;
} else {
push(@back, $a1);
}
$back[-1] !~ m!^\s*#! and $back[-1] =~ s!\\$!! and $is_cont = 1;
}
if ( scalar(@back) < scalar(@$in_aref) ) {
@back = &linearize(\@back);
}
return (@back);
}# linearize()
sub remove_comment {
my ($in_aref) = @_;
my $cnt = 0;
my @back;
for my $a1 (@$in_aref) {
$a1 =~ m!^\s*(#|$)! and do { &tsmsg("[Wrn] Skip unusable line [$a1]\n"); next; };
push(@back, $a1);
}
return(@back);
}# remove_comment()