如何在 tmux 会话中 运行 python 文件?

How to run python file inside tmux session?

我正尝试在 tmux 会话中 运行 python 脚本。我写了一个命令 (tmux new-session -d -s my_session),它比 crontab 好 运行ning。

但是当我尝试 运行 python 或 shell 文件时 tmux new-session -d -s my_session 'python3 test.pytmux new-session -d -s my_session 'sh test.sh 该脚本没有 运行。我使用了 中的参考。 请帮我解决这个问题。

编辑
可以用\;分隔tmux命令,然后使用send-keys命令将命令发送到active window.
在您的情况下,您可以使用:

tmux new-session -d -s my_session \; send-keys "python3 test.py" Enter
tmux new-session -d -s my_session \; send-keys "sh test.sh" Enter
tmux new-session -d -s my_session \; send-keys "python3 -m http.server 8080" Enter

您可以在 tmux manpages section for send-keys:

上找到有关 send-keys 选项的更多信息

send-keys [-lMRX] [-N repeat-count] [-t target-pane] key ...
(alias: send)
Send a key or keys to a window. Each argument key is the name of the key (such as ‘C-a’ or ‘NPage’) to send; if the string is not recognised as a key, it is sent as a series of characters. The -l flag disables key name lookup and sends the keys literally. All arguments are sent sequentially from first to last. The -R flag causes the terminal state to be reset.

-M passes through a mouse event (only valid if bound to a mouse key binding, see MOUSE SUPPORT).
-X is used to send a command into copy mode - see the WINDOWS AND PANES section.
-N specifies a repeat count.

send-keys 语法在 Key Bindings section of the tmux manpage 中有描述。 send-keys 使用的键名与 bind-key 使用的相同。

我通常在基本文件之上使用不同的配置文件。

假设您在 ~/.tmux.conf 中进行了 tmux 配置,然后我在 ~/.tmux/ 文件夹中创建了不同的配置文件。例如,我可以有一个 python 配置文件(如果你想进入会话,请使用 attach):

# To use this configuration launch tmux with the command:
#   > tmux -f ~/.tmux/python.conf attach
#

# Load default tmux config
source-file ~/.tmux.conf

# Create session and launch python script
new-session -s python -n python -d -c ~/src/python/
send-keys "python test.py" Enter

这让我可以灵活地创建更复杂的会话。