bash 退出 zsh,但让正在运行的作业保持打开状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19302913/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:18:14  来源:igfitidea点击:

Exit zsh, but leave running jobs open?

bashzshjobs

提问by Owen_R

Just switched from bash to zsh.

刚刚从 bash 切换到 zsh。

In bash, background tasks continue running when the shell exits. For example here, dolphincontinues running after the exit:

在 bash 中,后台任务会在 shell 退出时继续运行。例如在这里,dolphin在 之后继续运行exit

$ dolphin .
^Z
[1]+  Stopped                 dolphin .
$ bg
[1]+ dolphin . &
$ exit

This is what I want as the default behavior.

这就是我想要的默认行为。

In contrast, zsh's behavior is to warn about running jobs on exit, then close them if you exitagain. For example here, dolphinis closed when the second exit-command actually exits the shell:

相比之下,zsh 的行为是警告在 上运行作业exit,然后exit再次关闭它们。例如这里,dolphin当第二个exit-command 实际退出 shell时关闭:

 % dolphin .
^Z
zsh: suspended  dolphin .
 % bg
[1]  + continued  dolphin .
 % exit
zsh: you have running jobs.
 % exit 

How do I make zsh's default behavior here like bash's?

我如何使 zsh 的默认行为像 bash 一样?

回答by Anko

Start the program with &!:

启动程序&!

dolphin &!

The &!(or equivalently, &|) is a zsh-specific shortcut to both backgroundanddisownthe process, such that exiting the shell will leave it running.

&!或等效地,&|)是zsh的特定快捷方式既背景否认的过程中,使得离开壳会离开它运行。

回答by Carl Norum

From the zsh documentation:

zsh 文档

HUP

... In zsh, if you have a background job running when the shell exits, the shell will assume you want that to be killed; in this case it is sent a particular signal called SIGHUP... If you often start jobs that should go on even when the shell has exited, then you can set the option NO_HUP, and background jobs will be left alone.

HUP

... 在 zsh 中,如果您在 shell 退出时有一个后台作业正在运行,则 shell 会假设您希望它被杀死;在这种情况下,它被称为发送一个特定的信号SIGHUP......如果你经常启动应该去,即使外壳已经退出,那么你可以设置选项的工作NO_HUP,和后台作业将被单独留在家中。

So just set the NO_HUPoption:

所以只需设置NO_HUP选项:

% setopt NO_HUP

回答by ryanjdillon

I have found that using a combination of nohup, &, and disownworks for me, as I don't want to permanently cause jobs to run when the shell has exited.

我发现,使用的组合nohup&以及disown对我的作品,因为我不想永远当外壳已经退出原因的工作运行。

nohup <command> & disown

While just &has worked for me in bash, I found when using only nohup, &, or disownon running commands, like a script that calls a java run command, the process would still stop when the shell is exited.

虽然只是&对我来说有效的bash,我只使用时发现nohup&disown在运行命令,这样调用Java运行命令的脚本,该过程仍然会在外壳退出停止。

  • nohupmakes the command ignore NOHUPand SIGHUPsignals from the shell
  • &makes the process run in the background in a subterminal
  • disownfollowed by an argument (the index of the job number in your jobs list) prevents the shell from sending a SIGHUPsignal to child processes. Using disownwithout an argument causes it to default to the most recent job.
  • nohup使命令忽略NOHUPSIGHUP从外壳发出信号
  • &使进程在子终端的后台运行
  • disown后跟一个参数(作业列表中作业编号的索引)可防止 shellSIGHUP向子进程发送信号。disown不带参数使用会导致它默认为最近的作业。

I found the nohupand disowninformation at this page, and the &information in this SO answer.

我在此页面上找到了nohupdisown信息,以及此 SO 答案中的信息。&

回答by Aydin K.

I typically use screenfor keep background jobs running.

我通常screen用于保持后台作业运行。

1) Create a screen session:

1)创建一个屏幕会话:

screen -S myScreenName

2) Launch your scripts,services,daemons or whatever

2) 启动你的脚本、服务、守护进程或其他任何东西

3) Exit (detach) screen-session with

3)退出(分离)屏幕会话

screen -d

or shortcut ALT+A then d

或捷径 ALT+A then d



After few hundreds of years - if you want to resume your session (reattach):

数百年后 - 如果您想恢复会话(重新连接):

screen -r myScreenName

If you want to know if there's a screen-session, its name and its status (attached or detached):

如果你想知道是否有一个屏幕会话、它的名称和它的状态(附加或分离):

screen -ls


This solution works on all terminal interpreters like bash, zsh etc. See also man screen

此解决方案适用于所有终端解释器,如 bash、zsh 等。另见 man screen