How do I put an already-running process under nohu

2019-01-03 18:55发布

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?)

标签: shell nohup
10条回答
太酷不给撩
2楼-- · 2019-01-03 19:37

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.
查看更多
家丑人穷心不美
3楼-- · 2019-01-03 19:41

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.

查看更多
Deceive 欺骗
4楼-- · 2019-01-03 19:47

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

kill -20 PID 
kill -18 PID

kill -20 (SIGTSTP) will suspend the process and kill -18 (SIGCONT) will resume the process, in background. So now, closing both your terminals won't stop your process.

查看更多
走好不送
5楼-- · 2019-01-03 19:47

This worked for me on Ubuntu linux while in tcshell.

  1. CtrlZ to pause it

  2. bg to run in background

  3. jobs to get its job number

  4. nohup %n where n is the job number

查看更多
登录 后发表回答