{"id":150,"date":"2023-07-13T11:24:30","date_gmt":"2023-07-13T03:24:30","guid":{"rendered":"https:\/\/www.kz-hub.tech\/?p=150"},"modified":"2023-07-13T11:24:59","modified_gmt":"2023-07-13T03:24:59","slug":"%e4%bd%bf%e7%94%a8perl%e8%84%9a%e6%9c%ac%e6%89%b9%e9%87%8f%e8%bf%90%e8%a1%8c%e5%88%9b%e5%bb%ba%e7%9a%84shell%e8%84%9a%e6%9c%ac%e4%b8%ad%e7%9a%84%e6%89%80%e6%9c%89%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/www.kz-hub.tech\/index.php\/2023\/07\/13\/%e4%bd%bf%e7%94%a8perl%e8%84%9a%e6%9c%ac%e6%89%b9%e9%87%8f%e8%bf%90%e8%a1%8c%e5%88%9b%e5%bb%ba%e7%9a%84shell%e8%84%9a%e6%9c%ac%e4%b8%ad%e7%9a%84%e6%89%80%e6%9c%89%e5%91%bd%e4%bb%a4\/","title":{"rendered":"\u4f7f\u7528perl\u811a\u672c\u6279\u91cf\u8fd0\u884c\u521b\u5efa\u7684shell\u811a\u672c\u4e2d\u7684\u6240\u6709\u547d\u4ee4"},"content":{"rendered":"<p>parallel_run.pl \u811a\u672c\u4ee3\u7801<\/p>\n<pre><code class=\"language-perl\">#!\/usr\/bin\/perl -w\n\nuse strict;\nuse Getopt::Long;\nuse Cwd &#039;abs_path&#039;;\nuse threads;\n\nmy ($lines, $max) = (1, 100);\nGetOptions(\n    &quot;l:i&quot; =&gt; \\$lines,\n    &quot;m:i&quot; =&gt; \\$max,\n);\n\nmy $usage = qq{\nUsage: perl $0 [options] in.sh\n\nOptions:\n-l  # of lines of each job [$lines]\n-m  # of jobs to run concurrently [$max]\n};\n\ndie $usage unless(@ARGV);\n\nmy $dir = abs_path($ARGV[0]) . &#039;.workdir&#039;;\n`mkdir -p $dir`;\n\n#read input file and split\nmy $line_count = 0;\nmy $job_count = &#039;0001&#039;;\nmy $content = &#039;&#039;;\nmy (@script, @running_script, @running_script_name);\nwhile(&lt;&gt;){\n    next if(\/^\\s*$\/ || \/^\\s*#\/);\n    $line_count++;\n    $content .= $_;\n    if($line_count % $lines == 0){\n        &amp;output_to($content, &quot;$dir\/$job_count.sh&quot;);\n        $job_count++;\n        $content = &#039;&#039;;\n    }\n}\n&amp;output_to($content, &quot;$dir\/$job_count.sh&quot;) if($content);\n\n#run split scripts\nfor(my $i=0; $i&lt;@script; $i++){\n    &amp;join_all if(@running_script &gt;= $max);\n    my $tmp =  async { return system(&quot;sh $script[$i] &gt;$script[$i].o 2&gt;$script[$i].e&quot;); };\n    push @running_script, $tmp;\n    push @running_script_name, $script[$i];\n}\n&amp;join_all if(@running_script);\n\n## sub routines ##\nsub output_to{\n    my ($content, $dest) = @_;\n    push @script, $dest;\n    open OUT, &quot;&gt;$dest&quot; or die $!;\n    print OUT $content;\n    close OUT;\n}\n\nsub join_all{\n    system(&quot;echo -n &#039;start running @running_script_name at &#039;;date +&#039;%Y-%m-%d %H:%M:%S&#039;&quot;);\n    for(@running_script){ $_-&gt;join() }\n    (@running_script, @running_script_name) = ();\n    system(&quot;echo -n &#039;finish at &#039;;date +&#039;%Y-%m-%d %H:%M:%S&#039;&quot;);\n}<\/code><\/pre>\n<p>\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-bash\"># bash\nnohup .\/parallel_run.pl example.sh&gt;example.sh 2&gt;&amp;1 &amp;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>parallel_run.pl \u811a\u672c\u4ee3\u7801 #!\/usr\/bin\/perl -w use strict; use&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-150","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.kz-hub.tech\/index.php\/wp-json\/wp\/v2\/posts\/150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kz-hub.tech\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kz-hub.tech\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kz-hub.tech\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kz-hub.tech\/index.php\/wp-json\/wp\/v2\/comments?post=150"}],"version-history":[{"count":2,"href":"https:\/\/www.kz-hub.tech\/index.php\/wp-json\/wp\/v2\/posts\/150\/revisions"}],"predecessor-version":[{"id":152,"href":"https:\/\/www.kz-hub.tech\/index.php\/wp-json\/wp\/v2\/posts\/150\/revisions\/152"}],"wp:attachment":[{"href":"https:\/\/www.kz-hub.tech\/index.php\/wp-json\/wp\/v2\/media?parent=150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kz-hub.tech\/index.php\/wp-json\/wp\/v2\/categories?post=150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kz-hub.tech\/index.php\/wp-json\/wp\/v2\/tags?post=150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}