I want to use a single script to launch 2 sessions, a daemon session and a user session. I want the script to start upon boot without any users logged in.
The script I created works when launched directly, but only partially when started with systemctl start daemon.service as root (likewise at boot).
Essentially the script does this:
# Clean up any old tmux sessions
tmux kill-session -t daemon > /dev/null 2>&1
tmux kill-session -t user > /dev/null 2>&1
rm -rf /tmp/tmux-`id -u`
tmux new-session -d -s daemon
tmux send-keys "$DAEMON" C-m
# Start the main tmux session from which we'll create
# all window panes
export TMUX=
export TERM=xterm
tmux new-session -d -s user
tmux list-sessions >> $LOG
# Various window setup using "tmux split-window -h"
# or "tmux split-window -v" - no other args
# Window panes created. Now wait for daemon process to open socket, then
echo "Daemon is now listening." >> $LOG
tmux send-keys -t 1 "$CMD1" C-m
echo "Sent $CMD1 to pane 1" >> $LOG
tmux send-keys -t 1 "$CMD2" C-m
echo "Sent $CMD2 to pane 2" >> $LOG
...
# Spin in a loop until the daemon process stops listening, then exit
Thats it. Simple. No nested sessions, tho tmux warns nonetheless. Why? I read elsewhere that setting the TERM and unsetting the TMUX env vars is necessary for a systemd process as there is no tty for tmux to use. It did seem to help, tho I've gone thru so many trials couldn't give you any details.
The symptom is both sessions start, the daemon looks normal but the user session panes are empty, but all panes created properly. The send-keys don't appear to be sent to them, tho the log looks perfect, no hangs anywhere.
I need this to work on different versions of tmux from 1.9 to 2.1 (ubuntu 16.04 & Debian 8.7 & 8.8). The user portion starts a "less" pager to view the daemon log, and 2 processes that can interact with a user. I put a "tmux attach-session -t user" in my .profile so when I login I see all the windows and can interact with them. It's important that the user processes also start with the daemon even if no user is present.
I don't understand why tmux seems to think sessions are nested just b/c 2 are started from the same script. When the script exits something is wrong and systemd will then call the script again to restart everything. For testing I do have the #Restart=on-failure commented out.
I can see the send-keys are being executed by looking at ps, they're all running. I think the TMUX & TERM env vars are key to the problem but I'm not sure how to resolve it so A) tmux separates the sessions and B) there are no problems starting up with no users or open terminal ttys.