bash nohup 和 enter 将停止该过程:为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14634563/
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 and enter will stop the process: why?
提问by Ricky Robinson
My script needs root privileges and I want to run it on a remote machine and then be able to turn off my local computer.
我的脚本需要 root 权限,我想在远程机器上运行它,然后能够关闭我的本地计算机。
So I did: $ nohup sudo ./myScript arg1 &
所以我做了: $ nohup sudo ./myScript arg1 &
... which I always do, but on a different machine where I'm always root, so without sudo. For some reason now it's not working:
...我总是这样做,但在另一台我总是 root 的机器上,所以没有sudo. 由于某种原因,它现在不起作用:
nohup sudo ./myScript.sh 1 & //then I press enter twice
[8] 24264
me@my-laptop:~/myFolder$ nohup: ignoring input and appending output to `nohup.out'
[8]+ Stopped nohup sudo ./myScript.sh 1
What am I doing wrong here?
我在这里做错了什么?
回答by Barmar
Try this:
尝试这个:
sudo nohup ./script
After answering the password prompt, type Ctrl-zto suspend it, and bgto put it into the background.
回答密码提示后,键入Ctrl-z暂停它,并将bg其置于后台。
This is especially helpful if you are half-way through running a long process when you decide it needs to be run in the background.
如果您在运行一个长进程的中途决定需要在后台运行该进程时,这将特别有用。
回答by Digikata
One option here is use the screenutility. Start screen, run your script, detach using CTRL+A, D. Later, log back in, reconnect to the process by running screen again.
这里的一种选择是使用屏幕实用程序。启动 screen,运行脚本,使用 CTRL+A、D 分离。稍后,重新登录,再次运行 screen 重新连接到进程。
回答by weibeld
As mentioned in other answers, if a background process started with nohuptries to read from or write to the terminal, it is put into the "Stopped" state. The sudoprocess that you're executing tries to read your password from the terminal, so the process is stopped (for some reason, the reading seems to be triggered when Enter is pressed).
如其他答案中所述,如果后台进程以nohup尝试读取或写入终端开始,则会进入“已停止”状态。sudo您正在执行的进程尝试从终端读取您的密码,因此该进程停止(出于某种原因,按下 Enter 时似乎会触发读取)。
So, to avoid this, you ned to make sure to enter your password and become root before nohupis executed. For example, like this:
因此,为了避免这种情况,您需要确保在nohup执行之前输入您的密码并成为 root 。例如,像这样:
sudo bash -c 'nohup ./myScript.sh arg1 &'

