如何将正在运行的进程转换为 nohup 并保留日志文件

iam*_*ser 2 linux unix bash command-line-interface nohup

我有一个进程正在占用一个终端

]$ command some_argument
Run Code Online (Sandbox Code Playgroud)

我想从终端退出并回家,但同时我不想终止这个正在运行的进程。

对于上面的运行过程,我想实现如下:

]$ nohup command some_argument >& logfile &
Run Code Online (Sandbox Code Playgroud)

And*_*w B 5

对于大多数用户来说,取消进程从已经在后台运行的进程重定向 STDOUT 是不可行的。这个 SO 答案很好地涵盖了这个主题。

将进程与当前 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.
Run Code Online (Sandbox Code Playgroud)

如果你想了解更多关于什么nohupdisown实际做什么,你可以查看这个问题。(免责声明:无耻的自我推销)