我如何从一个用”go”编写的控制台应用程序转移到另一个控制台应用程序?

huangapple go评论74阅读模式
英文:

How can I transfer from a console app written in "go" to another console app?

问题

我想用"go"编写一个控制台应用程序,它将控制权转移到另一个控制台应用程序。

以下是步骤:

  1. 运行二进制应用程序,如果需要,验证并安装一些内容
  2. 二进制应用程序构建一个带有选项的命令字符串
  3. 二进制应用程序启动进程B并退出(在我的情况下是一个Node.js应用程序)
  4. 进程B运行完成并将输出发送到同一个控制台。

我唯一需要的步骤是启动一个新进程并退出,这就是这里的问题。

英文:

I want to write a console application using "go" that will transfer control to another console application.

Here are the steps:

  1. run binary app which validates and installs a few things if necessary
  2. binary app constructs a command string with options
  3. binary app starts process B and exits (a node.js app in my case)
  4. Process B runs to completion and sends output to the same console.

The ONLY step I need is the ability to start a new process and exit, so that is the question here.

答案1

得分: 1

os包看起来具有你可能需要的功能。函数StartProcess用于启动一个进程,函数(*Process) Kill用于退出进程。

英文:

The os package looks like it has the functionality you might need.
The function StartProcess for starting a process and the function (*Process) Kill to exit it.

答案2

得分: 1

os/exec可能会有帮助:

> 包exec用于运行外部命令。它包装了os.StartProcess,使得重新映射标准输入和标准输出、连接I/O管道和进行其他调整更加容易。

请查看Cmd.Start示例

英文:

os/exec might be helpful:

> Package exec runs external commands. It wraps os.StartProcess to make it easier to remap stdin and stdout, connect I/O with pipes, and do other adjustments.

Check out the Cmd.Start example.

答案3

得分: 1

请参考 https://stackoverflow.com/questions/10027477/golang-fork-process。简而言之,直接这样做存在问题。您应该使用非Go程序来启动您的node.js应用程序。

英文:

See https://stackoverflow.com/questions/10027477/golang-fork-process. In short, there are issues with doing this directly. You should instead use a non-Go program to start your node.js app.

huangapple
  • 本文由 发表于 2012年11月22日 06:15:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/13502957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定