4

I am just trying to teach myself GNU/Linux-style commands and tools in the bash terminal on my Mac.

I want the terminal to automatically display when a background process is complete. There is no specific program I am running, but, for example:

sleep 10 &

How do I (or is it even possible to) have it automatically display in the command line when sleep is done. Or do I just have to check manually?

3
  • 1
    as a note just pressing Enter in the very same terminal when the job has finished will display [1]+ Done sleep 1s for example, so please clarify if you are staying in the same terminal. Jan 14, 2020 at 20:57
  • Also check out the meaning of Linux Gnu Gnu/Linux. Linux is the kernel. I know people often call the whole think Linux, but this is just confusing: Gnu+Linux - Linux is .... Jan 14, 2020 at 22:42
  • 1
    … MacOS, according to the question. Yes, this question badly conflates a kernel that it isn't using, a shell that it probably isn't using (given that it is MacOS), and a terminal that isn't named what it names it and that isn't relevant to the question.
    – JdeBP
    Jan 15, 2020 at 0:08

2 Answers 2

3

set +b


The default mode. In this mode, every time bash is about to print its prompt, it first checks to see whether any background jobs have stopped or terminated, and if so it prints notification messages before displaying the prompt.

set +b never corrupts the display, but it has its own problems. In particular, suppose you are testing a GUI program whose source code you're editing in a separate editor window. (Let's assume it's written in an interpreted language.) You might have a terminal window in which you keep typing the same command, myprogram &. Every time you make changes to the program and want to restart it, you close down the current instance of it, and repeat this command in your shell window. Now bash will launch the new instance of the program before printing the notification that the old one has finished - and because printing that notification is the moment at which the job number allocation gets reset, this also means that your job numbers will increase without bound unless you deliberately hit Return at a point when no background job is running. Eventually, one instance of your program will hang and you'll have to type kill %134, instead of the kill %1 you would prefer.


set -b


In this mode, when any background job stops or terminates, bash receives a SIGCHLD signal, and its signal handler displays the notification message immediately - even if bash is currently in the middle of waiting for a foreground process to complete.

set -b has no awareness of the state of the terminal, so its notifications are likely to corrupt the display of any full-screen application you might be running (such as a text editor or MUA). In particular, this mode cannot even interact sensibly with bash's own command-line editing! Try running sleep 1 & in this mode, and then immediately beginning to type a command line. You will find that when the notification is printed, you are left with the display in a somewhat non-obvious state.


Source of all this found here.

2
  • Perfect, thanks! set -b is what I needed. I read through the source material and did some experimenting. Jan 14, 2020 at 23:34
  • 1
    @nightanimal If you found my answer helpful, please accept it, there is a checkmark under the up and down arrows. Glad to help. Cheers! Jan 15, 2020 at 1:17
1

By default, bash is usually set to tell you a background job has changed status only when a command prompt is ready to be displayed.

You can enable this synchronous notification to asynchronous with set -b. But it's not without its disadvantages - for example, it has no concept of cursor line position if you are currently entering a command.

Incidentally, when you're learning the command line tools, be aware that the ones installed on your Mac are subtly (and sometimes not so subtly) different to the ones with the same names on a GNU/Linux distribution.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .