Find the answer to your Linux question:
Results 1 to 7 of 7
Is there a way to detach a currently-running process from the curent tty?...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    139

    how to detach a running process?


    Is there a way to detach a currently-running process from the curent tty?

  2. #2
    Just Joined!
    Join Date
    Mar 2005
    Location
    Milano (Italy)
    Posts
    45
    you have to use the command screen

  3. #3
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    139
    Quote Originally Posted by debian_user
    you have to use the command screen
    switch to the terminal of where the command was launched from and type:
    Code:
    [CTRL-Z]
    bg [number]
    or just
    Code:
    bg
    , if you only launched one command from the current terminal

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Mar 2005
    Location
    Northern Ireland
    Posts
    23
    Further info : after running bg command to background the process you can then get a list of backgrounded jobs by typing
    Code:
    jobs
    ; and the status of each job (running, stopped, etc) For example

    Code:
     
    [dtodd@localhost dtodd]$ sleep 120
    <CTRL-Z here>
    &#91;1&#93;+  Stopped                 sleep 120
    &#91;dtodd@localhost dtodd&#93;$ bg
    &#91;1&#93;+ sleep 120 &
    &#91;dtodd@localhost dtodd&#93;$ jobs
    &#91;1&#93;+  Running                 sleep 120 &
    &#91;dtodd@localhost dtodd&#93;$
    To bring a process back to the foreground, use fg command; contuning the example:

    Code:
    &#91;dtodd@localhost dtodd&#93;$ fg
    sleep 120
    To kill a job in the background, get the job number and kill %<number>, ie, sleep jobs was number 1; so:

    Code:
    &#91;dtodd@localhost dtodd&#93;$ jobs
    &#91;1&#93;+  Running                 sleep 120 &
    &#91;dtodd@localhost dtodd&#93;$ kill %1
    &#91;dtodd@localhost dtodd&#93;$
    &#91;1&#93;+  Terminated              sleep 120
    &#91;dtodd@localhost dtodd&#93;$
    All good stuff - I hope it's useful

  6. #5
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,790
    Quote Originally Posted by debian_user
    you have to use the command screen
    either this or put an & at the end of the application. eg.
    Code:
    ntop &
    will run ntop in the background.

    Screen is more for applications where you want to see any output generated, but also want to background the process.
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

  7. #6
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    139
    Quote Originally Posted by sdousley
    Quote Originally Posted by debian_user
    you have to use the command screen
    either this or put an & at the end of the application. eg.
    Code:
    ntop &
    will run ntop in the background.

    Screen is more for applications where you want to see any output generated, but also want to background the process.
    I thought "debian_user" meant the "command prompt", because I don't have the "screen" utility installed.

  8. #7
    Just Joined!
    Join Date
    Nov 2008
    Posts
    2

    screen, disown

    I'm afraid none of the commands given so far, useful as they may be, actually detach a process from a tty. ^Z suspends a job, bg backgrounds it, and fg foregrounds it, but it's still attached. It can still print messages, and it will still be sent a termination signal if you disconnect the tty (close the window if you're using a graphical shell.)

    If you truly wish to detach a process, there are two ways to do it. The first is to use "screen." The advantage to this method is that screen essentially creates a second layer between the tty and the programs running inside of it, so you can detach and reattach at will, and without messing up the output or otherwise causing yourself headaches. You can even detach on one tty and reattach on a different one. Or several different ones.

    The problem with screen, of course, is that you have to start it *before* you start the process you will later wish to detach. If, by some mistake, you didn't do that, you're not completely out of luck. The other command you can use is "disown" This completely detaches the job from the tty. You can logout/close the window and the job will continue merrily along its way in the background. (be sure you bg it before you disown it. I'm not sure all systems would necessarily unsuspend it when you issue the disown command.

    The big disadvantage to disown is that it is exceedingly difficult, if not impossible to reattach the process to your tty if you decide you want it back. So use it with caution.

Posting Permissions

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