Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

To separate regular commands in Unix is to put semicolon in the end like this:

cd /path/to/file;./someExecutable;

But it seems not working for commands like this:

./myProgram1 > /dev/null &
./myProgram2 > /dev/null &
=>./myProgram1 > /dev/null &;./myProgram2 > /dev/null &;

Is there any way separate these kind of commands?

Also, if are below 2 cases are equivalent if I copy paste to command prompt? Thanks.

cd /path/to/file;./someExecutable;

cd /path/to/file;
./someExecutable;
share|improve this question
add comment

2 Answers 2

up vote 11 down vote accepted

Well the ";" makes the shell wait for the command to finish and then continues with the next command.

The "&" will send any process directly into the background and continues with the next command - no matter if the first command finished or is still running.

So "&;" will not work like you expect.

But actually I'm unsure what you expect.

Try this in your shell:

sleep 2 && echo 1 & echo 2 & sleep 3 && echo 3

it will output: 2 1 3

Now compare it with

sleep 2 ; echo 1 & echo 2 & sleep 3 ; echo 3

which will output 1 2 3

Regards.

share|improve this answer
1  
So if I want run 2 commands at the same time in the background. It should be ./myProgram1 > /dev/null & ./myProgram1 > /dev/null & right? –  Stan Sep 15 '10 at 16:45
1  
Yes. But actually it's not EXACTLY happening in the same nano-second :P –  Jan. Sep 16 '10 at 15:23
add comment

command1 & command2 Will execute command1 in the background and immediately begin executing command2, even if command1 has not completed.

command1 ; command2 Will execute command1 and then execute command2 once command1 finishes.

command1 && command2 will only execute command2 once command1 has completed execution successfully. If command1 fails, command2 will not execute.

share|improve this answer
    
Thanks for the clarification! –  Stan Sep 15 '10 at 16:45
    
No problem. Hope it helps! Though I'm not sure if it actually answers your original question. –  gWaldo Sep 15 '10 at 20:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.