我有一个进程当前占用一个终端
]$ command some_argument
我想退出终端并回家,但同时我不想终止这个正在运行的进程。
对于上面的运行流程,我想要实现类似下面的功能:
]$ nohup command some_argument >& logfile &
答案1
否认该过程和对于大多数用户来说,从已经运行在后台的进程重定向 STDOUT 是不可行的。这个答案很好地涵盖了该主题。
将进程与当前 shell 分离并不难:您正在寻找disown
。来自手册页:
SIGNALS
....
The shell exits by default upon receipt of a SIGHUP. Before exiting,
an interactive shell resends the SIGHUP to all jobs, running or
stopped. Stopped jobs are sent SIGCONT to ensure that they receive the
SIGHUP. To prevent the shell from sending the signal to a particular
job, it should be removed from the jobs table with the disown builtin
(see SHELL BUILTIN COMMANDS below) or marked to not receive SIGHUP
using disown -h.
....
SHELL BUILTIN COMMANDS
....
disown [-ar] [-h] [jobspec ...]
Without options, each jobspec is removed from the table of
active jobs. If jobspec is not present, and neither -a nor -r
is supplied, the shell's notion of the current job is used. If
the -h option is given, each jobspec is not removed from the ta‐
ble, 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 job‐
spec does not specify a valid job.
如果你想进一步了解nohup
具体disown
做法,可以查看这问题。(免责声明:无耻的自我推销)
答案2
我知道您目前正在使用基于 Linux 的操作系统,但是为了记录在案,以防有人碰巧使用 Solaris 10 或更新版本在寻找解决方案时找到此页面,Solarisnohup
实现了您正在寻找的内容:
$ command some_argument
...
^Z[1] + Stopped (SIGTSTP) command some_argument
$ bg
[1] command some_argument&
$ jobs -l
[1] + 1061 Running command some_argument
$ nohup -p 1061
Sending output to nohup.out
$ exit