Run Multiple Commands At Once in the Same Terminal

Run multiple commands at once in the same terminal

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

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";

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"

Example:

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

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

How to run multiple commands (each from a new terminal) from a batch file in Windows

You need to use e.g. START to spawn new processes, otherwise the currently running program will just block until it's finished. It looks to me that this is what you want:

@ECHO OFF
CD C:\Users\tanzeel\Downloads\social-coder
START code .
CD application
START npm start
CD ..\server
START node server.js
EXIT

How do I run two commands in one line in Windows CMD?

Like this on all Microsoft OSes since 2000, and still good today:

dir & echo foo

If you want the second command to execute only if the first exited successfully:

dir && echo foo

The single ampersand (&) syntax to execute multiple commands on one line goes back to Windows XP, Windows 2000, and some earlier NT versions. (4.0 at least, according to one commenter here.)

There are quite a few other points about this that you'll find scrolling down this page.

Historical data follows, for those who may find it educational.

Prior to that, the && syntax was only a feature of the shell replacement 4DOS before that feature was added to the Microsoft command interpreter.

In Windows 95, 98 and ME, you'd use the pipe character instead:

dir | echo foo

In MS-DOS 5.0 and later, through some earlier Windows and NT versions of the command interpreter, the (undocumented) command separator was character 20 (Ctrl+T) which I'll represent with ^T here.

dir ^T echo foo

Executing Multiple Commands in same shell Windows

Looks like I stumbled upon it with a bit of research, you can use the following on Windows:

exec.Command("cmd", "/C", "echo foo && echo bar")

How do I run multiple commands with python subprocess( ) without waiting for the end of each command?

Switch out your subprocess.run(command_lst) with Popen(command_lst, shell=True) in each of your scripts and and loop through the command list like the example below to run the processes in parallel.

This is how you implement Popen to run processes in parallel using arbitrary commands for simplicity.

from subprocess import Popen

commands = ['ls -l', 'date', 'which python']

processes = [Popen(cmd, shell=True) for cmd in commands]

How to run multiple commands within a single task in VSCode?

VSCode features Compound Tasks.

You define the steps of your build process as separate tasks and then reference them from the compound task.

Cannot run multiple commands in Java terminal

Java does not provide a shell, it just splits the string provided into words and passes it as arguments to the executable mentioned as first word.

Try passing the command to /bin/bash (or another shell of your choosing):

final String innerCommand = "g++ " + projPath +" -o " + name + " && ./" + name;
final String[] command = {"/bin/bash", "-c", innerCommand};
final Process process = Runtime.getRuntime().exec(command);

Run multiple commands simultaneously in bash in one line

Capturing SSH output

On the one hand, you need to capture the ssh output/error and store it into a file so that you can process it afterwards with Python. To this purpose you can:

1- Store output and error directly into a file

ssh user@node cmd 2>&1 > session.log

2- Show output/error in the console while storing it into a file (I would recommend this one)

ssh user@node cmd 2>&1 | tee session.log

Check this for further information about the tee command.

Running commands in parallel

On the other hand, you want to run both commands in parallel and block the current bash process. You can achieve this by:

1- Blocking the current bash process until their childs are done.

cmd1 & ; cmd2 & ; wait

Check this for further information about the wait command.

2- Spawning the child processes and freeing the current bash process. Notice that the processes will be kept alive although the main process ends.

nohup cmd & ; nohup cmd & 

The whole thing

I would recommend combining both approaches using tee (so you can still see the ssh outputs on your terminal) and blocking the current process until everything is done (so that when you kill the main process all the processes are killed too).

ssh user@node1 uptime 2>&1 | tee session1.log & ; ssh user@node2 uptime 2>&1 | tee session2.log & ; wait


Related Topics



Leave a reply



Submit