使用golang连接到一个包含存储库的屏幕会话时遇到问题。

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

Having issues attaching to a screen session with golang - repository inside

问题

我正在开发一个名为MSCT的小型Minecraft服务器管理工具,目前只是为个人使用。我在一个screen会话中启动服务器,一切都正常。但是我无法让screen会话附加或让MSCT恢复screen会话。

仓库在这里:https://github.com/nathanpaulyoung/msct/blob/master/msct.go

相关的函数在第48行:

resumeCommand()

以下是代码片段:

func resumeCommand() cli.Command {
	command := cli.Command{
		Name:    "resume",
		Aliases: []string{"r"},
		Usage:   "resume a server's screen session",
		Action: func(c *cli.Context) {
			servername := c.Args().First()
			screenname := buildScreenName(servername)
			args := []string{"-x", screenname}
			cmd := exec.Command("screen", args...)
			if serverExists(servername) {
				output, _ := cmd.Output()
				println(output)
			} else {
				println("No server known by the name \"" + servername + "\". Either server.jar is missing or the server directory was not configured before compilation.")
				os.Exit(999)
			}
		},
	}
	return command
}

我觉得可能是我不知道的某些特殊方法,比如一种特殊的方式来调用新的tty之类的。如果你们中有人知道解决办法,请提供建议或提交一个pull request。

话虽如此,我对真正的目标思考得更多了,我想提一下,以防这能带来一个同样令人满意的解决方案,尽管不是我期望的。*我希望Minecraft服务器在我不主动查看服务器控制台时仍然继续运行。*无论是在我当前没有通过ssh连接到运行服务器的主机上,还是(在使用screen的情况下)实际上没有连接到screen会话,它都应该在没有我的监视下继续运行。如果有一种方法,也许是将screen内容通过https://golang.org/x/crypto/ssh/terminal管道传输到自定义的golang终端会话中,或者创建一种类似于screen的东西,Minecraft服务器可以在其中运行,那将是同样令人满意的。

英文:

I'm working on MSCT, a little Minecraft server administration utility for personal use (for now). I'm starting the server in a screen session, and it's working just fine. I'm just having no luck getting the screen to start attached or getting MSCT to resume the screen.

Repo is here: https://github.com/nathanpaulyoung/msct/blob/master/msct.go

The relevant function on line 48 is:

resumeCommand()

And here's a snippet:

func resumeCommand() cli.Command {
	command := cli.Command{
		Name:    "resume",
		Aliases: []string{"r"},
		Usage:   "resume a server's screen session",
		Action: func(c *cli.Context) {
			servername := c.Args().First()
			screenname := buildScreenName(servername)
			args := []string{"-x", screenname}
			cmd := exec.Command("screen", args...)
			if serverExists(servername) {
				output, _ := cmd.Output()
				println(output)
			} else {
				println("No server known by the name \"" + servername + "\". Either server.jar is missing or the server directory was not configured before compilation.")
				os.Exit(999)
			}
		},
	}
	return command
}

I feel like it must be something I am simply unaware of, like some sort of special way to invoke a new tty or something. If any of you know of the solution, please chip in with advice or a pull request.

That said, I've been thinking more about the real goal here, and figured I'd mention that, in case that yields a solution that is equally satisfactory if not what I expected. I want the Minecraft server to continue running even when I'm not actively looking at the server console. Whether that means when I'm not currently ssh'd in to the box it's running on, or (in the case of screen) not actually connected to the screen session, it should keep running without my eyes on it. If there's a way to, perhaps, pipe screen content into a custom golang terminal session using https://golang.org/x/crypto/ssh/terminal, or a way to create something similar to screen wherein the Minecraft server can run, those would be just as satisfactory.

答案1

得分: 1

你需要创建一个分离的屏幕并在没有连接到屏幕会话的情况下执行文件。

要做到这一点,你需要使用 screen -dmS <screenName> <command> 命令。

你还需要将 cmd.Output() 更改为 cmd.Start(),因为使用后者你不需要等待命令结束(在屏幕的情况下,执行可能是无限的)。

参考这个链接:https://stackoverflow.com/questions/70144624/starting-a-program-in-screen-linux-using-golang

英文:

You need to create a screen detached and execute files without attached the screen session.

To do this, you must use screen -dmS &lt;screenName&gt; &lt;command&gt;

You need also to change the cmd.Output() to cmd.Start() because with the second you don't need to wait the end of command (in screen case, the execute could be infinite)

Check this: https://stackoverflow.com/questions/70144624/starting-a-program-in-screen-linux-using-golang

huangapple
  • 本文由 发表于 2016年2月16日 14:39:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/35425461.html
匿名

发表评论

匿名网友

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

确定