How do I put an already-running process under nohup?
Solution 1
Using the Job Control of bash to send the process into the background:
- Ctrl+Z to stop (pause) the program and get back to the shell.
-
bg
to run it in the background. -
disown -h [job-spec]
where [job-spec] is the job number (like%1
for the first running job; find about your number with thejobs
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
flybywire
Updated on December 19, 2021Comments
-
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 about 13 yearsAs 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 over 12 yearsHow do I recover the job later? I can see it running using ps -e.
-
rustyx almost 12 yearsYou 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 over 11 yearsis it possible somehow to do something like 'my_job_command | tee my_job.log' after the command is already running?
-
mbrownnyc about 11 years
disown
disassociates any pipes from the process. To reattach pipes, usegdb
as described in this thread. More specifically, this post. -
Cory Kendall almost 11 yearsWhy is a "jobspec" a job number prefixed with a
%
? I can't find this anywhere in documentation, can anyone give me a pointer? -
Izkata over 10 years@CoryKendall Here's some info on it.
-
Pungs over 10 yearsYes, thats a OS problem, kill does not work with Cygwin on Windows.
-
ADTC over 10 yearsThe
bg
command shows the job number already on screen (the number between square brackets, like[1]
). -
Amir Ali Akbari over 10 yearsAlso very useful if the job is started from another ssh session.
-
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 over 9 yearsJust remember to do the
disown %1
in the first terminal before closing it. -
Rune Schjellerup Philosof over 9 years
disown -a
removes all jobs. A simpledisown
only removes the current job. As the man page in the answer says. -
Michele over 9 yearsThis 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 runbg
! -
cmaster - reinstate monica almost 9 yearsVery 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 almost 9 years@cmaster You can check if a handle is redirected using
lsof
(the file handle's name ispipe
, not like/dev/pts/1
) or byls -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 almost 9 yearshow do you "reown" a process after disowning it?
-
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 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 over 6 yearsWeird: 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 almost 6 yearsOnly for
AIX
andSolaris
. "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 over 5 yearsYou shouldn't use hardcoded signal numbers; use names.
-
Mark Lopez over 5 years@jchevali you can request an edit if you feel that would add to the answer.
-
Pablo Bianchi over 5 yearsMore easy to remember is just
kill -SIGTSTP PID
andkill -SIGCONT PID
. -
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 about 5 yearsI 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 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 over 3 yearsThis 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 about 3 yearsThe best answer on how to bring a detached process back to a terminal! Thanks a lot!
-
Fernando Fabreti over 2 yearsCarefull. This way process is still bound to terminal and will terminate if terminal is closed.
-
Fernando Fabreti over 2 years
disown -h %1
seems to avoid SIGHUP (completely detach process from terminal). -
Jeff Hykin over 2 yearsThis 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