How to execute code in a new tmux session - from within current session?
If you put the code you want to execute in e.g. /opt/my_script.sh
, it's very easy to do what you want:
tmux new-session -d -s "myTempSession" /opt/my_script.sh
This starts a new detached session, named "myTempSession", executing your script. You can later attach to it to check out what it's doing, by executing tmux attach-session -t myTempSession
.
That is in my opinion the most straightforward and elegant solution. I'm not aware of any easy way of execute commands from stdin (read "from heredocs") with tmux. By hacking around you might even be able to do it, but it would still be (and look like) a hack.
For example, here's a hack that uses the command i suggested above to simulate the behaviour you want (= execute code in a new tmux session from a heredoc. No write occurs on the server's hard drive, as the temporary file is created /dev/shm
, which is a tmpfs):
(
cat >/dev/shm/my_script.sh &&
chmod +x /dev/shm/my_script.sh &&
tmux new-session -d '/dev/shm/my_script.sh; rm /dev/shm/my_script.sh'
) <<'EOF'
echo "hacky, but works"
EOF
Also see https://serverfault.com/questions/339390/run-command-in-detached-tmux-session for examples on using the send-keys command on a detached pane.