Home > Mobile >  How to make a background job always print "Done" in Bash?
How to make a background job always print "Done" in Bash?

Time:12-15

I've noticed that sometimes a background job does not print Done unless I press Enter.

Taking git gc for example,

git gc --force --prune=now &

The output is like,

Enumerating objects: 574964, done.
Counting objects: 100% (574964/574964), done.
Delta compression using up to 4 threads
Compressing objects: 100% (156394/156394), done.
Writing objects: 100% (574964/574964), done.
Total 574964 (delta 296425), reused 574891 (delta 296375), pack-reused 0
Removing duplicate objects: 100% (256/256), done.
Checking connectivity: 574964, done.
Expanding reachable commits in commit graph: 87696, done.

I don't know if it has really ended or not until I press Enter. If it's ended, it prints

[1]   Done                    git gc --force --prune=now

If I don't press Enter or any other key, it seems frozen and just does not print "Done" as if it's still running.

For a simple background job like echo hello &, it prints "Done" immediately after hello is printed.

[1] 17647
hello
[1]   Done                    echo hello

I'd like to know what leads to the different behaviors and how I can make it always print "Done" automatically when the job finishes. Thanks.

CodePudding user response:

According to man bash:

The shell learns immediately whenever a job changes state. Normally, bash waits until it is about to print a prompt before reporting changes in a job's status so as to not interrupt any other output. If the -b option to the set builtin command is enabled, bash reports such changes immediately.

  •  Tags:  
  • bash
  • Related