How can I automatically return to a bash prompt after printing the output from a function placed in the background?
For example, when I run the following script in the bash shell:
fn () {
sleep 10
echo "Done"
exit
}
fn & After running the script, it immediately returns my invitation. After 10 seconds, it prints โFinishโ and then displays a flashing cursor on a new line:
$ Done
โ
the script no longer works, but I do not receive the invitation back until I press Return .
Is there a way to force a bash request to return after printing "Finish"?
Related question: Is there a way for the background task to inform the terminal about printing a new invitation? However, this question is asked by the requested program. The answer attached to it relates to a program that is sent to the background, but does not seem to work for a function that is sent to the background (as in the example I provided).
To clarify: I want to save the entire code snippet above (for example, as myscript.sh ), and then run it as the foreground of the script (for example, as bash myscript.sh ).
EDIT . The above, of course, is just MWE. The context of this problem:
- User runs script
- Script sends the PBS job, starts the tail of the output file in the background, and calls
fn & - The user gets a hint back, can start to do other things.
- Job exit appears on user terminal when job starts
fn controls the queue and kills tail when the job ends.- Users complain that they do not return (i.e., press Enter ) after completion.
Here is a slightly less minimal code:
watch_queue () {
until [`qstat | grep $ job | wc -l` -lt 1]; do
sleep 2
done
kill -9 $ pid
tput setaf 7
tput setab 0
echo "Hit ENTER to return to your command prompt."
tput sgr0
exit 0
}
cmd = "something complicated that is built at runtime"
outfile = "ditto"
queue = "selected at runtime, too"
job = `echo" cd \ $ PBS_O_WORKDIR && $ cmd >> $ outfile "|
qsub -q $ queue -e / dev / null -o / dev / null |
awk 'BEGIN {FS = "." } {print $ 1} ''
echo "Job $ job queued on $ queue: $ cmd"
eval "tail -f -F $ outfile 2> / dev / null &"
pid = $!
watch_queue &
Of course, it would be much easier for me if my users could just get the task from a separate file or manage tasks between the background and the background on their own, but they cannot. They cannot even follow the instructions in the script to press Enter to get the โviewโ of the prompt back ... And I cannot open another โwindowโ - they do not have a display server.