bash "&" sin imprimir "[1]+ Listo "

I call a script in my .bashrc to print number of new messages I have when I open the terminal, I want the call to be non blocking as it accesses the network and sometimes takes a few seconds which means I can't use the terminal until it completes.

However if I put:

    mailcheck & 

in my bashrc, it works fine. but then prints an empty line and I have when I press enter and it prints

    [1]+  Done                    ~/bin/mailcheck

This is very messy is there a way around this?

preguntado el 03 de mayo de 12 a las 22:05

5 Respuestas

That message isn't coming from mailcheck, it's from bash's job control telling you about your backgrounded job. The way to avoid it is to tell bash you don't want it managed by job control:

mailcheck &
disown $!

contestado el 03 de mayo de 12 a las 22:05

¿Por qué no solo mailcheck & disown ? - Jonatan Öström

Esto parece funcionar:

(mailcheck &)

contestado el 04 de mayo de 12 a las 07:05

You can call your script like this:

(exec mailcheck & )

contestado el 03 de mayo de 12 a las 22:05

still no good I get: [1]+ Done exec ~/bin/mailcheck - ricardo mosse

It is working fine in my testing. When I include a script call in my .bashrc I don't get any Done message when I log-in next time. - anubhava

@anubhava: Can I use this technique in my Bashrc to source all scripts in a given directory as follows: List=(~/bin/Completion*); for File in "${List[@]}"; do (exec . "$File" &) done Pero me sale este error: bash: exec: .: cannot execute: Is a directory - Puerco espín

try piping stderr to /dev/null

mailcheck & 2>/dev/null

Thinking about it for a few mins, another way might be to use write.
Pipe the output of the background task to yourself, that way it can complete at any time and you can bin any additional output from stdout as well as stderr.

mailcheck | write $(whoami) & > /dev/null

contestado el 03 de mayo de 12 a las 22:05

No sorry should have said I tried that, it doesn't seem to be stderr - ricardo mosse

shouldn't be stderr because it's not an error. since it's running in the background it's just letting you know that the process has finished successfully. maybe stdout? - Matt K

That's no good either, I don't understand this, if it's not stdout or stderr what is it? - ricardo mosse

hmm... the output isn't from the script itself so it wouldn't be stdout. well I'm not sure, but since you found the answer another way it's all good! :) - Matt K

actually you still need to press enter as it hangs on a blank line is there a way around this? - ricardo mosse

This was the last page I checked before I fixed an issue I was having so I figured I would leave my finished script that had the same issue as OP:

nohup bash -c '{anycommand};echo "CommandFinished"' 1> "output$(date +"%Y%m%d%H%M").out" 2> /dev/null & disown

Esto corre {anycommand} con nohup y envía el stdout to a unique file, the stderr a /dev/null, and the rest to console (the PID) for scraping. The stdout file is monitored by another process looking for the CommandFinished or whatever unique string.

Bash would later print this:

[1]+  Done                    nohup bash -c ....

Adición disown to the end stopped bash jobs from printing that to console.

Respondido el 18 de Septiembre de 21 a las 01:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.