forked from vikas0633/perl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_forker.pl
190 lines (138 loc) · 4.47 KB
/
cmd_forker.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env perl
=head1 NAME
asmbl_cmd_forker.pl
=head1 SYNOPSIS
USAGE: asmbl_cmd_forker.pl -L asmbl_list_file -f command_list_file -n num_simultaneous_processes
=head1 OPTIONS
########################################################
#
# -L filename of list of asmbls to process
#
# -f filename of commands to run on each asmbl (place ASMBL where \$asmbl should be)
#
# -n number of processes to run simultaneously. (default 1 at a time)
#
# -d debug
# -S verbose
# -h print this and exit.
#
#########################################################
=head1 DESCRIPTION
This script allows you to run a pipeline of commands on a list of asmbl_ids.
For each asmbl_id in the asmbl_list_file (-L), each command in the command_file (-f) is
run.
If you have multiple CPUs, you can process more than one asmbl_id simultaneously, using -n option.
=head1 INPUT
-L asmbl_list_file: a list of asmbl_ids in a file, one asmbl_id per line.
-f command_file: a list of commands. Put the token 'ASMBL' where the asmbl_id would be substituted.
=head1 OUTPUT
A directory is created called acf.logs.$pid, which contains the stdout and stderr generated by each command and for each asmbl_id.
=head1 CONTACT
Brian Haas
=begin comment
this section doesn't show up in viewers but is used for parsing.
status: active
keywords: pipeline processing
=end comment
=cut
use strict;
require "getopts.pl";
use vars qw ($opt_f $opt_S $opt_h $opt_n $opt_L $list $opt_d $DEBUG $splink @ids @cmds $cmd @x $asmbl);
$|=1;
&Getopts('dD:L:Sp:f:hn:');
if ($opt_h) {
die <<_EOH_;
########################################################
#
# -L filename of list of asmbls to process
#
# -f filename of commands to run on each asmbl (place ASMBL where \$asmbl should be)
#
# -n number of processes to run simultaneously.
#
# -d debug
# -S verbose
# -h print this and exit.
#
#########################################################
_EOH_
}
if (length($opt_L) > 0) { $list = $opt_L; } else { &option_die;}
if (length($opt_d) > 0) { $DEBUG = 1; } else { $DEBUG = 0; }
my ($SEE) = ($opt_S) ? 1:0;
my ($number_processes_allowed) = ($opt_n) ? $opt_n : 1;
my $commands;
unless ( $commands = $opt_f) {die;}
# read in list of assemblies
open (LIST, $list) || die "Cant open $list";
while ($splink = <LIST>) {
$splink =~ s/\s//g;
print "SPLINK: $splink \n" if ($DEBUG);
print "A: $splink\n" if ($DEBUG);
push(@ids, $splink);
}
close(LIST);
## Read in command lines
print "Reading Commands\n" if $SEE;
open (COM, "$commands");
my $line;
while ($line = <COM>) {
if ($line =~ /^\#/) {next;} #ignore commented out command lines.
if ($line =~ /\w/) {
print "Read Command: $line" if $SEE;
chomp $line;
push (@cmds, $line);
}
}
close COM;
my $counter = 0;
my $LOGDIR = "acf.logs.$$";
unless (-d $LOGDIR) {
mkdir $LOGDIR or die $!;
chmod (0775, $LOGDIR);
}
print "Total number of processes allowed at one time: $number_processes_allowed\n" if $SEE;
foreach $asmbl (@ids) {
if ($asmbl) {
print "\n------- Processing asmbl: $asmbl\n";
if ($counter >= $number_processes_allowed) {
print "\tWaiting for a child to exit\n\n" if $SEE;
wait();
}
$counter++;
my $whoami = fork();
if (!$whoami) {
#system "sleep 3"; #pause between launches for humanity's sake.
print "\nRunning process $counter under child\n\n" if $SEE;
my $output_log = "$LOGDIR/$asmbl.stdout";
my $errlog = "$LOGDIR/$asmbl.errlog";
open (OUTLOG, ">$output_log");
open (STDOUT, ">&OUTLOG");
open (ERRLOG, ">$errlog");
open (STDERR, ">&ERRLOG");
open (RET, ">$LOGDIR/$asmbl.ret");
my @copy_cmds = @cmds;
my $cmd_num = 0;
foreach my $cmd (@copy_cmds) {
$cmd_num++;
$cmd =~ s/ASMBL/$asmbl/g;
print "CMD (asmbl: $asmbl, cmd_num: $cmd_num): $cmd\n";
my $ret = system $cmd;
print RET "ret($ret)\t$cmd\n";
}
close ERRLOG;
close OUTLOG;
close RET;
# remove emtpy files
unless (-s $output_log) { unlink $output_log;}
unless (-s $errlog) { unlink $errlog;}
exit(0);
}
}
}
print "End of Parent Process\nWaiting for children to exit\n" if $SEE;
while (wait () > 0) {}
sub option_die {
die;
}