并行运行shell脚本 [英] Running shell script in parallel

查看:223
本文介绍了并行运行shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个shell脚本

I have a shell script which


  1. 洗牌大文本文件(6万行和6列)

  2. 文件排序依据的第一列

  3. 输出1000个文件

所以伪code看起来像这样

So the pseudocode looks like this

file1.sh 

#!/bin/bash
for i in $(seq 1 1000)
do

  Generating random numbers here , sorting  and outputting to file$i.txt  

done

有没有一种方法来运行平行这个shell脚本充分利用多核CPU的?

Is there a way to run this shell script in parallel to make full use of multi-core CPUs?

目前, /file1.sh 在执行顺序1到1000的运行,这是非常缓慢的。

At the moment, ./file1.sh executes in sequence 1 to 1000 runs and it is very slow.

感谢您的帮助。

推荐答案

查看庆典子shell ,这些可用于并行运行的脚本的部分。

Check out bash subshells, these can be used to run parts of a script in parallel.

我没有测试过这一点,但是这可能是一个开始:

I haven't tested this, but this could be a start:

#!/bin/bash
for i in $(seq 1 1000)
do
   ( Generating random numbers here , sorting  and outputting to file$i.txt ) &
   if (( $i % 10 == 0 )); then wait; fi # Limit to 10 concurrent subshells.
done
wait

这篇关于并行运行shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆