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 ?
Run the command in a subshell:
(sleep 2 &)
jobs
will show nothing).
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".
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.
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.