English 中文(简体)
以 unix 形式平行运行任务
原标题:parallel running of jobs in unix

我有很多工作(例如)要用 unix (bash shell) 集束电脑运行:

### job1
mkdir file01
cp *.map  flex01
date > out
cd ..

### job2
mkdir file02
cp *.map  flex02
date > out
cd ..

### job3
mkdir file03
cp *.map  flex03
date > out
cd ..

### job4
mkdir file04
cp *.map  flex04
date > out
cd ..

如果我提交这些工作,我们一个接一个地做。但是我想同时运行,这意味着工作1到工作4同时运行。

抱歉问这么简单,我是新来的

最佳回答

cd 命令似乎不是一个好主意;您不会将 cd 附加到目录中。 您可能还想要将日期信息附加到输出文件, 而不是总是屏蔽它。 您似乎更可能将地图文件复制到刚刚创建的目录中。 因此, 您可能会写入 :

(mkdir flex01; cp *.map flex01; echo "Job 1: $(date)" >> out) &
(mkdir flex02; cp *.map flex02; echo "Job 2: $(date)" >> out) &
(mkdir flex03; cp *.map flex03; echo "Job 3: $(date)" >> out) &
(mkdir flex04; cp *.map flex04; echo "Job 4: $(date)" >> out) &

wait

此操作将命令的每个序列作为单独的背景工作运行, 然后在继续之前等待全部完成 。 您也可以查看对此项任务使用循环 。

for n in $(seq 1 4)
do
    (mkdir flex0$n; cp *.map flex0$n; echo "Job $n: $(date)" >> out) &
done

您也可以考虑使用 mkdir - pliver01 , 这样您在试图创建已经存在的目录时不会收到错误信息 。 (或者您可以测试错误而不复制, 或者在运行 mkdir 之前测试是否存在错误, 或者在复制前清理错误信息, 或者...)

问题回答

您可以在您的命令行上附加 , 从而启动每个命令并将其放入背景。 I. e.,

date > out &

由于您正在单列的目录中启动每个命令, 使用相同名称来抓取输出的文件不会有问题 。

Update: Based on the additional information provided below in the comments, I believe your problem lies elsewhere and I would suggest you contact the site s helpdesk and/or look at some site-specific sample script files to help you set up your parallel jobs in the most effective way for the environment you are working in.

如果您安装了GNU平行 http://www.gnu.org/software/parallel/" rel="nofollow" >http://www.gnu.org/software/parallel/ ,您可以这样做:

seq 4 | parallel  mkdir flex0{}; cp *.map flex0{}; echo "Job {}: $(date)" >> out 

每个CPU核心将运行一个进程。

您可以通过以下方式安装 GNU 平行 :

wget http://git.savannah.gnu.org/cgit/parallel.git/plain/src/parallel
chmod 755 parallel
cp parallel sem

Watch the intro videos for GNU Parallel to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1





相关问题
Generate assembler code from C file in linux

I would like to know how to generate assembler code from a C program using Unix. I tried the gcc: gcc -c file.c I also used firstly cpp and then try as but I m getting errors. I m trying to build an ...

Function to create the array by reading the file

I am creating scripts which will store the contents of pipe delimited file. Each column is stored in a separate array. I then read the information from the arrays and process it. There are 20 pipe ...

Compare characters at the end of the string C++

This program supposed to find command line arguments entered on Unix which ends with “.exe”. For some reason it doesn t work. Here is the code: int main( int argc, char* argv[] ) { for ( int ...

Batch Job Dependencies Using Open Source/Free Software

I run a large data warehouse plant where we have a lot of nightly jobs running concerruently however many have dependencies on a extract or data load process before they start. Currently we use an ...

Writing application for both Unix and Windows

I ll write a program for Interactive UNIX (http://en.wikipedia.org/wiki/INTERACTIVE_UNIX). But in a year it will be ported to Windows. I ll write it in ANSI C and/or SH-script. When it runs on Windows ...

Development Environment in Windows [closed]

What are your recommendations for setting up a development environment in Windows, especially when not using an IDE. I am attempting to familiarize myself with Windows, and I feel a bit lost. What ...

热门标签