如何使用一个命令创建一个具有 4 个窗口的 GNU 会话,每个窗口运行不同的命令?

如何使用一个命令创建一个具有 4 个窗口的 GNU 会话,每个窗口运行不同的命令?

我想让屏幕在每次启动时都在后台启动。我需要至少有 4 个窗口,每个窗口都会自动运行不同的命令。我该怎么做?

答案1

首先,启动一个分离的屏幕会话。然后使用其名称向该会话发送命令。请注意,没有好的方法可以确定会话是否已在运行和/或其中发生了什么;您必须确保不要在脚本已运行时尝试重新启动它(有很多方法可以做到这一点,但这超出了这个问题的范围)。

我建议将命令行放入脚本中,否则您可能会在引用级别时遇到麻烦。假设您已经这样做了,您的启动器脚本将如下所示:

#!/bin/bash

# An arbitrary name to uniquely identify this screen session:
SESSION="my_session_name_here"

# Create the detached session, running the first command in its first window:
screen -S "${SESSION}" -d -m script1.sh

# Now add the other windows. You create new windows within screen with
# "C-a:screen" (usually mapped to some other shortcut), so that's the command: 

screen -S "${SESSION}" -X screen script2.sh
screen -S "${SESSION}" -X screen script3.sh

# You could have given the windows different titles by adding "-t title1" etc.

您可以随时附加会话以查看其正在执行的操作,就像您以交互方式启动它一样。但是,由于每个窗口中的命令不是您从中启动相应脚本的 shell,因此只要您按 Control-C 退出脚本,窗口就会关闭。如果您想避免这种情况,请记住,screen 会话的行为就像您手动与其交互一样,因此您可以执行以下操作:

screen -S my_session -d -m
# "active" window is now 0 (the only one)
screen -S my_session -X exec script1.sh
screen -S my_session -X screen
# "active" window is now 1, running the shell
screen -S my_session -X exec script2.sh
# ... etc.

然而,这有点脆弱。如果你真的用了这个,你确实想要有一个非常简单的包装脚本,其中只列出屏幕远程命令,并将所有实际工作放在其他脚本中,即使它们非常短。

答案2

脚本bash可以启动任何程序,无论是命令行还是 GUI。您需要做的就是为每个应用程序创建一行。如果您使用,nohup您可以在所有应用程序启动后退出 shell:-

#!/bin/bash
nohup Program1Path Program1Parameters&
nohup Program2Path Program2Parameters&
nohup Program3Path Program3Parameters&
nohup Program4Path Program4Parameters&
exit

一旦您创建了该脚本并使其可执行,只需将其添加到Startup Applications,它将在您登录时运行。

相关内容