bash Nohup 在 OSX 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9450854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nohup does not work in OSX
提问by Mr. Demetrius Michael
Bash:
重击:
$nohup sleep 10 &
but when I close the terminal:
但是当我关闭终端时:


Any ideas? ideally I want to run Mongodb in the background.
有任何想法吗?理想情况下,我想在后台运行 Mongodb。
采纳答案by mpontillo
Interesting. Seems like an issue specific to the default Terminal app. Because for what it's worth, iTerm2doesn't exhibit this behavior. (so in other words, nohupis not actually broken on OS X; this just seems like special behavior in Terminal which looks for subprocesses on exit.)
有趣的。似乎是默认终端应用程序特有的问题。因为就其价值而言,iTerm2不会表现出这种行为。(换句话说,nohup在 OS X 上实际上并没有被破坏;这似乎是终端中的特殊行为,它在退出时寻找子进程。)
In many ways, iTerm2 it's better than the default terminal. You should give it a try!
在很多方面,iTerm2 都比默认终端好。你应该试一试!
回答by Bill
I tried this on Snow Leopard, and the dialog popped up and complained that it was going to kill sleep, but when I checked via ps -eafsleep was still running.
我在雪豹上试过这个,弹出对话框并抱怨它会杀死睡眠,但是当我通过ps -eaf睡眠检查时,它仍在运行。
bill$ ps -eaf | grep sleep
501 11806 1 0 0:00.00 ?? 0:00.01 sleep 1000
501 11811 2628 0 0:00.00 ttys001 0:00.00 grep sleep
回答by Jon Shier
Running it in a screensession should work just fine.
在screen会话中运行它应该可以正常工作。
回答by Jose Alban
I'm on OSX 10.8.5, and can confirm:
我在 OSX 10.8.5 上,可以确认:
nohup base64 /dev/urandom &
nohup base64 /dev/urandom &
Terminal.app-based apps (eg TotalTerminal) spawns the process under the shell session, not under launchd, which would be the expected/equivalent to linux behaviour.
基于 Terminal.app 的应用程序(例如 TotalTerminal)在 shell 会话下生成进程,而不是在 下launchd,这将是预期的/等效于 linux 行为。
On the other hand, iTerm2.app was able to run the same command under launchd, and it kept alive after the shell session was closed. It implements some special trick though:
另一方面,iTerm2.app 能够在 下运行相同的命令launchd,并且在 shell 会话关闭后它保持活动状态。不过它实现了一些特殊的技巧:
─┬◆ 00001 root /sbin/launchd
├─┬◆ 00245 albanj01 /sbin/launchd
│ └─┬◆ 21533 albanj01 /Applications/iTerm.app/Contents/MacOS/iTerm2 -psn_0_94628409
│ └─┬◆ 04684 albanj01 /Applications/iTerm.app/Contents/MacOS/iTerm2 --server /Applications/iTerm.app/Contents/MacOS/iTerm2 --launch_shell
│ └─┬◆ 04685 albanj01 -zsh
│ └──◆ 04759 albanj01 base64 /dev/urandom
My colleague on my side has tried the same thing on OSX 10.10.x and Terminal.app spawned the nohupprocess under launchd, suggesting that potentially they have fixed it between 10.8.x-10.10.x .
我这边的同事在 OSX 10.10.x 上尝试了同样的事情,并且 Terminal.app 在nohup下产生了这个过程launchd,这表明他们可能已经在 10.8.x-10.10.x 之间修复了它。
回答by user12365116
use tmux
使用 tmux
brew install tmux
tmux
use "tee" command to store output to txt
使用“tee”命令将输出存储到txt
python hello.py | tee -a ~/Downloads/output.txt

