BashScripts.org
http://bashscripts.org/forum/

how to move background process to foreground
http://bashscripts.org/forum/viewtopic.php?f=15&t=1011
Page 1 of 1

Author:  batola [ Fri Mar 05, 2010 5:09 pm ]
Post subject:  how to move background process to foreground

Hi,

is it possible in bash to run one command in background and after it's done show output with less? While background process is running I should be able enter another commands.

What I have is this (sleep is only for simulation):

Code:
{({ ( tree / > tree.txt && sleep 2)  & } && wait && less tree.txt) &} && wait && fg &


Problem is that I don't know and I can't find how to move it to the foreground after completion.

What I want is:

1) redirect output from `tree` to tree.txt in background
2) wait while `tree` is running
3) show tree.txt with `less`

All those steps should be run in background and after 1st step is done 3rd step should be brought to foreground.

Thanks.

Author:  Watael [ Fri Mar 05, 2010 7:09 pm ]
Post subject:  Re: how to move background process to foreground

it seems to be non-sense: how could you see anything from the less command when it's in background?!

Author:  batola [ Sat Mar 06, 2010 2:14 am ]
Post subject:  Re: how to move background process to foreground

That's why I want it to bring foreground.

Author:  Watael [ Sat Mar 06, 2010 4:24 am ]
Post subject:  Re: how to move background process to foreground

just don't put it background

Author:  batola [ Sat Mar 06, 2010 11:43 am ]
Post subject:  Re: how to move background process to foreground

Sure :), but as I said I need it on background.

Author:  Watael [ Sat Mar 06, 2010 5:18 pm ]
Post subject:  Re: how to move background process to foreground

may be:
Code:
fg %$(jobs | sed '/less tree\.txt/s/\[\(.\).*/\1/')

please, know that it still looks strange.

Author:  batola [ Sun Mar 07, 2010 4:30 am ]
Post subject:  Re: how to move background process to foreground

thanks, but it doesn't work

Author:  Watael [ Sun Mar 07, 2010 6:37 am ]
Post subject:  Re: how to move background process to foreground

How do you do it?
Do you have any error message?

This one works for me, now:
Code:
{({ ( tree > tree.txt && sleep 2)  & } && wait && \less tree.txt) & } && fg %$(jobs | sed -n '/less tree\.txt/s/\[\(.\).*/\1/p')
as I don't have `tree' installed I used `pstree' instead, I hope they behave the same

Author:  batola [ Sun Mar 07, 2010 9:59 am ]
Post subject:  Re: how to move background process to foreground

sure that works (it will work just with fg, too), but I need run whole comand in bg and it doesn't work. try:

Code:
{({ ( tree > tree.txt && sleep 2)  & } && wait && \less tree.txt) & } && fg %$(jobs | sed -n '/less tree\.txt/s/\[\(.\).*/\1/p') &

Author:  Watael [ Sun Mar 07, 2010 11:07 am ]
Post subject:  Re: how to move background process to foreground

are you trolling?
what are all those background requirements for?

I'll may be rude, but you're definetly nut: you're backgrounding the foreground command ! it surely won't ever work.

but tell us when you get things done...

KISS

Author:  batola [ Sun Mar 07, 2010 12:30 pm ]
Post subject:  Re: how to move background process to foreground

I know it's absolutely pointless, but I got this issue in school :|. My example command is just what it should be.

IMHO, in real world I would open screen set up bell in one window and work in another freely. Unfortunately my tearch probably can't describe issue (I have done it school without backgrounding and he had said it's bad).

Author:  Watael [ Sun Mar 07, 2010 2:02 pm ]
Post subject:  Re: how to move background process to foreground

If what your teacher asked is
Code:
# redirect output from `tree` to tree.txt in background
tree > tree.txt &
# wait while `tree` is running
wait
# show tree.txt with `less`
less tree.txt
why should it be more complicated?
otherwise, please provide your teacher's complete requirements.

Author:  batola [ Sun Mar 07, 2010 2:05 pm ]
Post subject:  Re: how to move background process to foreground

I described his requirements in first entry. Maybe he just wanted:

Code:
{ tree > output.txt & } && wait &


Continue to work with other commands and after done notify run less.

Anyway thanks for your help.

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com