Results 1 to 5 of 5

Thread: bash: run multiple parallel threads

  1. #1
    Join Date
    Sep 2008
    Beans
    Hidden!
    Distro
    Ubuntu 18.04 Bionic Beaver

    bash: run multiple parallel threads

    Hello Dears

    I have a script where I run a sequence of commands like this

    Code:
    #!/bin/bash
    
    # Working directory 
    WDIR="/home/user/dir"
    
    command_1
    
    command_2
    
    command_3
    
    command_4

    Now this way the Bash will wait the end of command_1 before to start command_2. Since all this commands are independent from each other I would rather like to have them starting simultaneously before I have a quad core and each of these commands will use a single core.

    There is any way to run independent commands in parallel rather than in sequence?

    Thanks in advance for your help

    Best Wishes
    Pietro

  2. #2
    Join Date
    Apr 2010
    Beans
    88
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: bash: run multiple parallel threads

    You could put each command into the background using the & operator, they'd be completly independent and run seperatly while the main script continued. Probably not ok if you want more control of the threads, e.g. knowing when they all finish, but may do the job.

    e.g:

    command_1 &

    command_2 &

    etc

  3. #3
    Join Date
    Apr 2010
    Beans
    88
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: bash: run multiple parallel threads

    Continued ....

    Actualy can probably use "jobs" and/or "wait" commands in main script so it can wait at the end until all background jobs have finished if you want.

  4. #4
    Join Date
    Sep 2008
    Beans
    Hidden!
    Distro
    Ubuntu 18.04 Bionic Beaver

    Re: bash: run multiple parallel threads

    That was not the actual scritp, it was just an example. I can do what you say but I need something aouthomatic.

    Best
    Pietro


    Quote Originally Posted by dethorpe View Post
    You could put each command into the background using the & operator, they'd be completly independent and run seperatly while the main script continued. Probably not ok if you want more control of the threads, e.g. knowing when they all finish, but may do the job.

    e.g:

    command_1 &

    command_2 &

    etc

  5. #5

    Re: bash: run multiple parallel threads

    I just went through this but using yad. Show a progress meter while my function did it's thing (that took 11s to do).

    I find help on Unix.com/shell-programming-scripting. It is an invaluable resource.
    Windows assumes the user is an idiot.
    Linux demands proof.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •