Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run Commands Simultaneously 1

Status
Not open for further replies.

TamedTech

IS-IT--Management
May 3, 2005
998
GB
Hello Guys,

I’m a little new to the whole Shell scripting process but I’m slowly learning.

I’m looking for a method to run two commands simultaneously in a script, what’s the best way of achieving this? Perhaps use something like the ‘while’ command?

Thanks,

Rob
 
You can use & to run commands in the background

while [ a = b ];do
netstat -rn &
ifconfig -a &
done

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
Ok that sounds good thanks mike,

So presumably I can add as many commands into that as i like and just add an & at the end and it will run them all at the same time?

Thanks,

Rob
 
Well not exactly at the same time, but as close as.

Just as point of interest have a look at the man page for nohup as well

man nohup

nohup allows your commands to kept running even if you disconnect your terminal session.

nohup command &

other useful operators are && and ||

ls && ps

&& allows the second command if the first was successful

so

zzzzzzz && ps

would produce an error zzzzzzz not found and would not run ps, handy if you want to make sure the first thing worked before you start the second.

ls || ps

would only run ls

zzzzzzz || ps

would run ps because zzzzzzz failed. Useful to clean up if things go wrong.

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
Thats excelent thanks mike,

I didnt end up requireing the WHILE statement, just stuck the & at the end of the commands i wanted to run and it ran them almost along side one another.

Achieved my goal nicely.

Thanks,

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top