Linux shell 同时运行编程

标签 linux

我需要运行一个程序,假设 ./a 在 linux shell 中运行 10 次。这样做的命令是什么?

另外,如何区分不同进程的输出?

此外,我的 ./a 是一个持续的过程。我怎么能终止它?

谢谢。

最佳答案

在 bash 中你可以这样做:

for ((i=1;i<=10;i++)); do

  # run the command ./a and redirect its output to a file named after
  # its sequence number...like out1.txt, out2.txt...
  ./a > out$i.txt &   

done

这里的关键是在后台运行命令的&。如果没有 &,第二个 ./a 将在第一个完成后启动,有效地使其串行执行。但是使用 &,我们不需要等待 ./a 的一个调用完成,然后再启动另一个调用,这就是我们实现并行性的方式。

关于Linux shell 同时运行编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3904396/

相关文章:

python - fnmatch 不适用于变量,但适用于静态字符串

c++ - 解决 Linux 库依赖关系?

c - 我对读取事件的 inotify_add_watch 有疑问

linux - Yum 存储库 - 找不到正确的存储库

linux - shell脚本中的参数处理

php - 安装 Composer 时出现语法错误?

html - 是否有类似 Webmaster's Toolkit 的东西可以在 Linux 上运行?

linux - 期待脚本和 ssh

linux - 看了相关帖子,在没有root权限的Linux上还是无法安装GCC 4.7?

linux - 多个任务等待同一个信号量