Bash script to start another script in a tmux session

bashtmux

I have a bash script that continuously outputs some information when run. I need to

  1. Automatically run this when my system boots.
  2. Monitor this output and control it every once in a while remotely, using ssh.

For this purpose, I would like to use tmux. So how do I approach this? For simplicity, let's say my shell script is this:

filename: start.bash

#!/bin/bash
# just an example for simplicity    
watch date

I need another script that runs this in tmux and be able to attach to this when I need, later. I am struggling at the part where I need to create a new tmux session with a name and make it run another shell script. Once I have this working, I can put this in another shell script and take care of the rest of the stuff. That is easy, I think. Can someone give me an example for this specific step please?

Best Answer

You can do this many ways.

You can do it after you've created the session either with send-keys:

tmux new -s "remote" -d
tmux send-keys -t "remote" "start.bash" C-m
tmux attach -t "remote" -d

Or through the shell:

tmux new -s "remote" -d "/bin/bash"
tmux run-shell -t "remote:0" "start.bash"
tmux attach -t "remote" -d
Related Question