Register
It is currently Sat Nov 01, 2014 5:04 am

how to move background process to foreground


All times are UTC - 6 hours


Post new topic Reply to topic  [ 13 posts ] 
Author Message
 PostPosted: Fri Mar 05, 2010 5:09 pm   

Joined: Fri Mar 05, 2010 5:07 pm
Posts: 7
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.


Top
 Profile  
 PostPosted: Fri Mar 05, 2010 7:09 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 552
it seems to be non-sense: how could you see anything from the less command when it's in background?!


Top
 Profile  
 PostPosted: Sat Mar 06, 2010 2:14 am   

Joined: Fri Mar 05, 2010 5:07 pm
Posts: 7
That's why I want it to bring foreground.


Top
 Profile  
 PostPosted: Sat Mar 06, 2010 4:24 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 552
just don't put it background


Top
 Profile  
 PostPosted: Sat Mar 06, 2010 11:43 am   

Joined: Fri Mar 05, 2010 5:07 pm
Posts: 7
Sure :), but as I said I need it on background.


Top
 Profile  
 PostPosted: Sat Mar 06, 2010 5:18 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 552
may be:
Code:
fg %$(jobs | sed '/less tree\.txt/s/\[\(.\).*/\1/')

please, know that it still looks strange.


Top
 Profile  
 PostPosted: Sun Mar 07, 2010 4:30 am   

Joined: Fri Mar 05, 2010 5:07 pm
Posts: 7
thanks, but it doesn't work


Top
 Profile  
 PostPosted: Sun Mar 07, 2010 6:37 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 552
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


Top
 Profile  
 PostPosted: Sun Mar 07, 2010 9:59 am   

Joined: Fri Mar 05, 2010 5:07 pm
Posts: 7
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') &


Top
 Profile  
 PostPosted: Sun Mar 07, 2010 11:07 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 552
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


Top
 Profile  
 PostPosted: Sun Mar 07, 2010 12:30 pm   

Joined: Fri Mar 05, 2010 5:07 pm
Posts: 7
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).


Top
 Profile  
 PostPosted: Sun Mar 07, 2010 2:02 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 552
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.


Top
 Profile  
 PostPosted: Sun Mar 07, 2010 2:05 pm   

Joined: Fri Mar 05, 2010 5:07 pm
Posts: 7
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.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP