简体   繁体   English

如何从终端启动命令,以便终端不是父节点?

[英]How do I start a command from terminal so that terminal is not the parent?

Let's take example of a command "example-command". 让我们举一个命令“example-command”的例子。

  1. I open terminal 我打开终端
  2. I write example-command in terminal, and example-command executes. 我在终端中编写example-command ,并执行example-command
  3. Now if I close terminal, example-command gets killed too. 现在,如果我关闭终端, example-command也会被杀死。
  4. I now try with " example-command & ", but the same behaviour. 我现在尝试使用“ example-command & ”,但行为相同。

How do I execute a command so that when I close the terminal, the command doesn't get terminated? 如何执行命令以便在关闭终端时命令不会终止?

There are two ways, identical in result. 有两种方式,结果相同。

  1. Use nohup when you start your program. 启动程序时使用nohup Eg, nohup example-command . 例如, nohup example-command You can background and work with it normally; 您可以正常背景和使用它; it will simply continue running after you've quit. 退出后它会继续运行。
  2. Alternatively, as @alamar noted , if you use bash as your shell, you can us the disown command. 或者, 正如@alamar所说 ,如果你使用bash作为你的shell,你可以使用disown命令。 Unfortunately, as far as I know, disown is bash-specific; 不幸的是,据我所知, disown是特定于bash的; if you use another shell, such tcsh , you may be restricted to the nohup form above. 如果你使用另一个shell,比如tcsh ,你可能会被限制在上面的nohup表单中。

Please search for similar questions first. 请先搜索类似的问题。

Besides the ways listed above, you can do: 除了上面列出的方法,您还可以:

setsid command_name

For example: 例如:

setsid xclock

Thanks 谢谢

In Zsh (not bash) you can: 在Zsh(不是bash)中你可以:

example-command &; disown {pid}

or just 要不就

example-command &; disown

You could also consider using the screen command. 您还可以考虑使用screen命令。

nohup example-command

您还可以使用'at'或'batch'命令并为其指定当前时间。

disown is a bash builtin. disown是一个bash内置。 You could create a wrapper shellscript for your command such as 您可以为您的命令创建包装shellcript,例如

#!/bin/bash
$1 &
P=`which $1`
disown `pidof ${P}`

Not the most robust script (by any means) but may help get you going. 不是最强大的脚本(无论如何),但可能有助于你前进。 For example: 例如:

$./launch_script.sh myProgram

You can also do this in the source of the program if you are editing it. 如果您正在编辑它,也可以在程序源中执行此操作。

Run: example-command 执行命令example-command

Press: Control-Z 按:Control-Z

Run: bg 运行:bg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM