如何完全分离一个进程

Nei*_*ill 5 bash process nohup disown

我想生成一个进程并在顶层运行它,这样当 shell 退出时,进程继续运行。

我错误地认为 nohup 会这样做,但是当我退出 shell(shell 是一个 lxc 容器,我 ssh 进入该容器)并重新进入时,该过程消失了。

似乎(查看 pstree)该进程仍在调用 shell 下,即使使用 nohup 或 disown。

我尝试了一些选项,setsid 似乎有效 - 但是我在这里用 nohup 做错了吗?

$ cat myproc
sleep 1234

$ myproc &
$ pstree -a | grep -B 3 "1234" | grep -v grep
  |-screen 
  |   |-bash -ls
  |   |   |-bash -ls
  |   |   |   `-sleep 1234


$ nohup myproc &
$ pstree 
  |-screen 
  |   |-bash -ls
  |   |   |-pstree -a
  |   |   `-sh ./myproc
  |   |       `-sleep 1234

$ myproc &
$ disown $!
$ pstree -a | grep -B 3 "1234" | grep -v grep
  |-screen 
  |   |-bash -ls
  |   |   |-bash -ls
  |   |   |   `-sleep 1234

$ setsid myproc &
  |-sh ./myproc
  |   `-sleep 1234
Run Code Online (Sandbox Code Playgroud)

成功。但是为什么 nohup 没有达到同样的效果呢?有没有办法更正确地使用 nohup?