46

If I run a command in the background with &, like this:

sleep 2 &

when the command finishes, I get "Done". How I can avoid seeing the "Done" message ?

4 Answers 4

43

Run the command in a subshell:

(sleep 2 &)
1
  • please note that by using this, you loose control of the jobs inside caller shell (jobs will show nothing).
    – mighq
    Jul 6, 2015 at 9:44
43

Execute the shell built-in:

$ set +m

This works by turning off "monitor mode" so the shell doesn't report terminating background jobs.

Although running the command in a subshell like:

$ (sleep 2&)

...will also disable the message, the only reason it works is because monitor mode is enabled by default only for interactive shells. That is, the subshell avoid the message by running an extra shell that has an automatic "set +m".

1
  • 5
    One should note, that this output is not performed by the sleep. Its the bash shell that just informs you about the finished background job.
    – flolo
    Jul 4, 2011 at 3:34
10

I'd like to clarify the two earlier answers. If what you want is never to see the Done message from any commands in your shell, set +m is the way to go. Just put it in your .profile and/or .bashrc and be done. Note however, that if you type this:

set +m
sleep 2 &
set -m

and the sleep ends after the final set -m, you will still get the done message.

If you want to disable the message for a single command invocation, the subshell technique (sleep 2 &) is the way to go.

In all honesty, I only knew about set +m, so +1 to Wooble for enlightening me. However, it is worth noting that which of the two solutions you want depends on what you are trying to do.

1

Without a subshell, you can do the following:

silent_background() {
    { 2>&3 "$@"& } 3>&2 2>-
    disown &>/dev/null  # Close STD{OUT,ERR} for silence if job has already completed
}

silent_background sleep 5

Based on this answer.

You must log in to answer this question.

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