I use Ubuntu 16.04 and I need the following tmux solution because I want to run a timeout process with sleep as in my particular case I wasn't satisfied from at and encountered a bug with nohup (when combining nohup-sleep). Now, tmux seems as best alternative as it has its own no-hangup mechanism and is actually working fine in manual usage (I ask the question only in regards to automizing the process I can already do manually with it).What I need:I need a way to do the following 3 actions, all in one operation:Attaching a new tmux session.Injecting a ready set of commands to that session, like (sleep 30m ; rm -rf dir_name ; exit). I would especially prefer a multi-line set, and not one long row.Executing the above command set the moment it was finished to be written as stdin in new tmux session.In other words, I want to execute a code set in another tmux session that was specially created for that cause, but to do all in one operation.Notes:I aim to do all from my original working session (the one I work from most of the time). Generally, I have no intention to visit the newly created session, I just want to create it with its automatically executed code and that's it.If possible, I would prefer an heredoc solution. I think it's most efficient.
How to execute code in a new tmux session - from within current session?
tmux;timeout;here document
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.shThis 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 worksEOF