Find the answer to your Linux question:
Results 1 to 4 of 4
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 ...
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    1

    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.

  2. #2
    Just Joined!
    Join Date
    Feb 2010
    Posts
    4

    Thumbs up

    Use the command 'fg' to bring the background process to foreground.

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    As far as I know, you cannot.

    By backgrounding a process, you are intentionally disassociating it from a terminal. Therefore, that process cannot possibly print anything to a terminal.

    You could write a program to wait for the first to finish, but this program would need a terminal, so it could not be backgrounded, which would mean that you could not enter other commands.


    On second thought, I suppose you could write a program that spawns a child that runs the command you want and then less. The initial program could exit immediately, freeing the terminal for input, but the child would still output to the terminal, so "less" could run. However, the terminal would behave very strangely once "less" was invoked (I don't believe that "less" would receive any input), and the program is awfully complicated for what you want.

    So as far as I know, this can't really be done.
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi, batola.

    Welcome to the forums.

    Your code and outline seem to be your apparent solution to a problem.

    Often, other solutions may be revealed by stepping back and stating the problem that you are trying to solve ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...