How do I put an already-running process under nohup?

486,920

Solution 1

Using the Job Control of bash to send the process into the background:

  1. Ctrl+Z to stop (pause) the program and get back to the shell.
  2. bg to run it in the background.
  3. disown -h [job-spec] where [job-spec] is the job number (like %1 for the first running job; find about your number with the jobs command) so that the job isn't killed when the terminal closes.

Solution 2

Suppose for some reason Ctrl+Z is also not working, go to another terminal, find the process id (using ps) and run:

kill -SIGSTOP PID 
kill -SIGCONT PID

SIGSTOP will suspend the process and SIGCONT will resume the process, in background. So now, closing both your terminals won't stop your process.

Solution 3

The command to separate a running job from the shell ( = makes it nohup) is disown and a basic shell-command.

From bash-manpage (man bash):

disown [-ar] [-h] [jobspec ...]

Without options, each jobspec is removed from the table of active jobs. If the -h option is given, each jobspec is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP. If no jobspec is present, and neither the -a nor the -r option is supplied, the current job is used. If no jobspec is supplied, the -a option means to remove or mark all jobs; the -r option without a jobspec argument restricts operation to running jobs. The return value is 0 unless a jobspec does not specify a valid job.

That means, that a simple

disown -a

will remove all jobs from the job-table and makes them nohup

Solution 4

These are good answers above, I just wanted to add a clarification:

You can't disown a pid or process, you disown a job, and that is an important distinction.

A job is something that is a notion of a process that is attached to a shell, therefore you have to throw the job into the background (not suspend it) and then disown it.

Issue:

%  jobs
[1]  running java 
[2]  suspended vi
%  disown %1

See http://www.quantprinciple.com/invest/index.php/docs/tipsandtricks/unix/jobcontrol/ for a more detailed discussion of Unix Job Control.

Solution 5

Unfortunately disown is specific to bash and not available in all shells.

Certain flavours of Unix (e.g. AIX and Solaris) have an option on the nohup command itself which can be applied to a running process:

nohup -p pid

See http://en.wikipedia.org/wiki/Nohup

Share:
486,920
flybywire
Author by

flybywire

Updated on December 19, 2021

Comments

  • flybywire
    flybywire over 2 years

    I have a process that is already running for a long time and don't want to end it.

    How do I put it under nohup (that is, how do I cause it to continue running even if I close the terminal?)

  • Dr. Jan-Philip Gehrcke
    Dr. Jan-Philip Gehrcke about 13 years
    As the question was how to "put it under nohup",disown -h perhaps is the more exact answer: "make disown behave more like nohup (i.e. the jobs will stay in your current shell's process tree until you exit your shell) This allows you to see all the jobs that this shell started." (from [quantprinciple.com/invest/index.php/docs/tipsandtricks/unix‌​/…)
  • Paulo Casaretto
    Paulo Casaretto over 12 years
    How do I recover the job later? I can see it running using ps -e.
  • rustyx
    rustyx almost 12 years
    You can't see the output of a job after a disown, disown makes a process a daemon, which means standard input/output are redirected to /dev/null. So, if you plan to disown a job, its better to start it with logging into a file, e.g. my_job_command | tee my_job.log
  • arod
    arod over 11 years
    is it possible somehow to do something like 'my_job_command | tee my_job.log' after the command is already running?
  • mbrownnyc
    mbrownnyc about 11 years
    disown disassociates any pipes from the process. To reattach pipes, use gdb as described in this thread. More specifically, this post.
  • Cory Kendall
    Cory Kendall almost 11 years
    Why is a "jobspec" a job number prefixed with a %? I can't find this anywhere in documentation, can anyone give me a pointer?
  • Izkata
    Izkata over 10 years
    @CoryKendall Here's some info on it.
  • Pungs
    Pungs over 10 years
    Yes, thats a OS problem, kill does not work with Cygwin on Windows.
  • ADTC
    ADTC over 10 years
    The bg command shows the job number already on screen (the number between square brackets, like [1]).
  • Amir Ali Akbari
    Amir Ali Akbari over 10 years
    Also very useful if the job is started from another ssh session.
  • Rooster
    Rooster over 9 years
    @Node according to this: unix.stackexchange.com/questions/3886/… disowned processes will fail if the terminal closes and the program tries to read from standard input or write from standard output.
  • fred
    fred over 9 years
    Just remember to do the disown %1 in the first terminal before closing it.
  • Rune Schjellerup Philosof
    Rune Schjellerup Philosof over 9 years
    disown -a removes all jobs. A simple disown only removes the current job. As the man page in the answer says.
  • Michele
    Michele over 9 years
    This is useful since I started a graphical interface with a console (in my case I started from konsole kwin after a crash without thinking of the consequences). So if I stopped kwin, everything would have frozen and I had no possibilities to run bg!
  • cmaster - reinstate monica
    cmaster - reinstate monica almost 9 years
    Very informative indeed, and likely to work well in simple cases. But be warned, more complex cases may fail miserably. I had one of those today: My process had spawened another process that did the output (presumably to stderr), but had stdout hooked up for communication with its master. Redirecting the masters FDs was effectless because the child had inherited stderr, and closing the childs stdout failed the master that was waiting on the other end of the pipe. X-| Bettern know your processes well before you try this.
  • TrueY
    TrueY almost 9 years
    @cmaster You can check if a handle is redirected using lsof (the file handle's name is pipe, not like /dev/pts/1) or by ls -l /proc/<PID>/fd/<fd> (this shows the symlink of the handle). Also subprocesses still can have not redirected outputs, which should be redirected to a file.
  • dylnmc
    dylnmc almost 9 years
    how do you "reown" a process after disowning it?
  • Nobody
    Nobody about 8 years
    @fred I didn't do this and it seemed to continue running. Possible or did I hit the wrong PID?
  • dylnmc
    dylnmc almost 7 years
    @KedarMhaswade fg resumes a process when you send it to background. E.g., while :; do sleep 5; echo "hooray"; done Enter bg %1 Enter fg %1. This doesn't work for disown, which "[removes] jobs from current shell"
  • Stefan Reich
    Stefan Reich over 6 years
    Weird: After disown -h %1, I still see the process's output in the shell. The process does seem to be properly nohup'd; I can close the shell and it keeps running. This is on Ubuntu 16 LTS.
  • AlikElzin-kilaka
    AlikElzin-kilaka almost 6 years
    Only for AIX and Solaris. "The AIX and Solaris versions of nohup have a -p option that modifies a running process to ignore future SIGHUP signals. Unlike the above-described disown builtin of bash, nohup -p accepts process IDs.". Source
  • jchevali
    jchevali over 5 years
    You shouldn't use hardcoded signal numbers; use names.
  • Mark Lopez
    Mark Lopez over 5 years
    @jchevali you can request an edit if you feel that would add to the answer.
  • Pablo Bianchi
    Pablo Bianchi over 5 years
    More easy to remember is just kill -SIGTSTP PID and kill -SIGCONT PID.
  • Kelthar
    Kelthar about 5 years
    @StefanReich According to this unix.stackexchange.com/questions/3886/… , disown only prevents the SIGHUP signal from being sent to the process when the shell is terminated. However, it doesn't prevent the proccess from attempting to use stdin/stdout to read from/write to the shell. In this case, the correct answer would be the one presented by Dale.
  • Kelthar
    Kelthar about 5 years
    I think the behaviour for disown -a is to cut attachment with all jobs. However, it doesn't shutdown the stdin/stdout pipelines, which means that the process will still try to write (/read) from the terminal
  • access_granted
    access_granted almost 5 years
    -h switch is unavailable in RHEL. However, even w/out using it, the process appears to be running ok as an orphan.
  • dotokija
    dotokija over 3 years
    This is very useful, since sometimes, especially with nohup <command> & CTRL+Z doesn't work. So, I vote for this as the right answer. No need to disown, all terminals could be closed safely.
  • shaheen g
    shaheen g about 3 years
    The best answer on how to bring a detached process back to a terminal! Thanks a lot!
  • Fernando Fabreti
    Fernando Fabreti over 2 years
    Carefull. This way process is still bound to terminal and will terminate if terminal is closed.
  • Fernando Fabreti
    Fernando Fabreti over 2 years
    disown -h %1 seems to avoid SIGHUP (completely detach process from terminal).
  • Jeff Hykin
    Jeff Hykin over 2 years
    This did not work for me in zsh. It paused the process (same as ctrl+Z) but then -SIGCONT in the new tty only causes it to be resumed in the foreground of the original tty. This is using zsh 5.8