Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fork two programs to the background using one line of bash and possibly an ampersand?

I have two programs that run indefinitely. I know I can fork one to the background then run the other by typing this at the command line:

> program1 &
> program2 &

However, I'm lazy and don't want to hit enter, I just want to run them both immediately. But bash complains when I do this:

> program1 &; program2 &

How can I run them both at the same time?

like image 573
Cory Klein Avatar asked Apr 19 '12 14:04

Cory Klein


People also ask

What does the ampersand do in bash?

An ampersand does the same thing as a semicolon or newline in that it indicates the end of a command, but it causes Bash to execute the command asynchronously. That means Bash will run it in the background and run the next command immediately after, without waiting for the former to end.

How do I run something in the background in bash?

To send a running command in the background: If users have already started a certain command and while they were using their system, their command-line blocks up, then they can suspend the execution of their currently foregrounded process by using “ctrl+z” for windows and “command+z” for mac systems.

How do I run a parallel script?

To run script in parallel in bash, you must send individual scripts to background. So the loop will not wait for the last process to exit and will immediately process all the scripts.


2 Answers

You leave out the ';' char, i.e.

 program1 &  program2 &

I hope this helps.

like image 148
shellter Avatar answered Sep 21 '22 19:09

shellter


try

(program1 &) ; (program2 &)
like image 44
pizza Avatar answered Sep 23 '22 19:09

pizza