在同一个终端同时运行多个并行命令 [英] Run parallel multiple commands at once in the same terminal

查看:1059
本文介绍了在同一个终端同时运行多个并行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要运行一些命令,每个不退出,直到按Ctrl-C是pssed $ P $。有什么我可以运行在一次运行,所有的人,和Ctrl-C将退出所有这些?他们可以共享端子输出。

I want to run a few commands, each of which doesn't quit until Ctrl-C is pressed. Is there something I can run to run all of them at once, and Ctrl-C will quit them all? They can share the terminal output.

具体来说,我有指南针编译器,编译器的CoffeeScript,而手表文件的自定义命令更改所有正在运行的看文件更改。我不想加载每个命令的终端。

Specifically, I have the compass compiler, coffeescript compiler, and a custom command that watches for file changes all running watching for file changes. I don't want to load up a terminal for each command.

推荐答案

本bash脚本是N个并行的线程。每个参数是一个命令。

This bash script is for N parallel threads. Each argument is a command.

陷阱当SIGINT被逮住将杀死所有的子进程。结果
等待$ pid_list相同正在等待每一个过程来完成。
当所有的进程都完成后,程序退出。

trap will kill all subprocesses when SIGINT is catched.
wait $PID_LIST is waiting each process to complete. When all processes have completed, the program exits.

#!/bin/bash

for cmd in "$@"; do {
  echo "Process \"$cmd\" started";
  $cmd & pid=$!
  PID_LIST+=" $pid";
} done

trap "kill $PID_LIST" SIGINT

echo "Parallel processes have started";

wait $PID_LIST

echo
echo "All processes have completed";

保存此脚本,如 parallel_commands ,并使其可执行。结果
这是如何使用这个脚本:

Save this script as parallel_commands and make it executable.
This is how to use this script:

parallel_commands "cmd arg0 arg1 arg2" "other_cmd arg0 arg2 arg3"

例如:

parallel_commands "sleep 1" "sleep 2" "sleep 3" "sleep 4"

启动4个并行的睡眠和等待,直到睡4结束。

Start 4 parallel sleep and waits until "sleep 4" finishes.

这篇关于在同一个终端同时运行多个并行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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