在 tmux 会话中启动另一个脚本的 bash 脚本

cod*_*kix 4 bash tmux

我有一个 bash 脚本,它在运行时连续输出一些信息。我需要

  1. 当我的系统启动时自动运行它。
  2. 使用 ssh 监视此输出并每隔一段时间远程控制它。

为此,我想使用 tmux。那么我该如何处理呢?为简单起见,假设我的 shell 脚本是这样的:

文件名:start.bash

#!/bin/bash
# just an example for simplicity    
watch date
Run Code Online (Sandbox Code Playgroud)

我需要另一个在 tmux 中运行它的脚本,并且可以在以后需要时附加到它。我在需要创建一个带有名称的新 tmux 会话并使其运行另一个 shell 脚本的部分苦苦挣扎。一旦我有了这个工作,我就可以把它放在另一个 shell 脚本中并处理其余的东西。这很容易,我想。有人可以给我一个这个特定步骤的例子吗?

jdw*_*olf 6

您可以通过多种方式做到这一点。

您可以在使用发送密钥创建会话后执行此操作:

tmux new -s "remote" -d
tmux send-keys -t "remote" "start.bash" C-m
tmux attach -t "remote" -d
Run Code Online (Sandbox Code Playgroud)

或者通过shell:

tmux new -s "remote" -d "/bin/bash"
tmux run-shell -t "remote:0" "start.bash"
tmux attach -t "remote" -d
Run Code Online (Sandbox Code Playgroud)