user3509081
user3509081

Reputation: 55

running 2 sh files at the same time

I would like to run 2 sh files at the same time. Can you guys let me know how to do it?

Example: I have a.sh file and b.sh file and I'd like to run these files at the same time with thread.

Upvotes: 1

Views: 1318

Answers (2)

tripleee
tripleee

Reputation: 189337

Run at least one of them in the background. That's a separate process, not a separate thread; but you couldn't really run different programs in the same process anyway.

./a.sh &
./b.sh
wait

This will start up a.sh in the background, and run b.sh in the foreground. In case that finishes first, we also wait for the background process to finish.

There are various issues with output buffering etc which usually confuse newbies. You should probably find a tutorial or FAQ about background jobs before you ask more questions. Here are some Google hits:

Upvotes: 1

jimm-cl
jimm-cl

Reputation: 5412

Hm, if you want them to be run at the very same exact time, then that's probably not that simple... I'm not even sure if possible. The best I can think of is:

./a.sh ; ./b.sh

They will be executed one after the other, and depending on how long it takes a.sh to finish it could be a while. The other option would be to use a scheduler, like cron or at - probably there's some magic at OS level that allow the two scripts to run at the same time if scheduled that way.


Edit

From the comments, I have corrected the syntax error caused by & ;. Thanks for pointing that out.

Regarding the other comments, I think that the commands are being ran sequentially, as stated in the bash readme:

Commands separated by a ; are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed.

Therefore, ./a.sh ; ./b.sh will execute a.sh first, wait until it finishes, and then run b.sh.

Upvotes: 0

Related Questions