Running bash commands in the background without printing job and process ids

前端 未结 8 1354
余生分开走
余生分开走 2020-12-07 22:09

To run a process in the background in bash is fairly easy.

$ echo \"Hello I\'m a background task\" &
[1] 2076
Hello I\'m a background task
[1]+  Done             


        
8条回答
  •  没有蜡笔的小新
    2020-12-07 22:13

    Sorry for the response to an old post, but I figure this is useful to others, and it's the first response on Google.

    I was having an issue with this method (subshells) and using 'wait'. However, as I was running it inside a function, I was able to do this:

    function a {
        echo "I'm background task $1"
        sleep 5
    }
    
    function b {
        for i in {1..10}; do
            a $i &
        done
        wait
    } 2>/dev/null
    

    And when I run it:

    $ b
    I'm background task 1
    I'm background task 3
    I'm background task 2
    I'm background task 4
    I'm background task 6
    I'm background task 7
    I'm background task 5
    I'm background task 9
    I'm background task 8
    I'm background task 10
    

    And there's a delay of 5 seconds before I get my prompt back.

提交回复
热议问题