How to launch tmux with pre-opened second window and execute commands? - bash

There's possible duplicate but it's closed and unanswered.
As I'm using chef for automation, would be nice to automate tmux launch with pre-launched python web server and second window opened. (this is specifically for my development environment). And only way of doing this probably specifying parameters from command line.
Commands which I want to execute are in window with title "daemon":
source bin/activate
cd project
DEBUG=1 python app.py
I'm unable to find which command line parameters allows to pre-execute commands when launching tmux, and also to open more windows on launch.

You want to create a session without attaching to it (using the -d option), so that you can send additional tmux commands to open the second window before actually attaching.
tmux new-session -t mysession -d 'source bin/activate; cd project; DEBUG=1 python app.py'
tmux new-window -t mysession
tmux attach-session -t mysession

Related

Why tmux fails to run a bash script when using a bind-key

The script using the normal command ./<script.sh> in a terminal window works without any problem. It troughs error returned 127 when running a bash script with bind-key:
tmux version 3.3a
bind-key -r i run-shell "tmux neww \<script path\>"
I have also tried the run the script from tmux command prompt using the :shell-run 'path/script/script.sh command it still does nothing.
Could someone explain what could be the issue. I was wondering if it could be related to tmux's default sh shell. The script uses #!/usr/bin/bash and not #!/usr/bin/sh.
The run:shell command still fails when I the script to #!/usr/bin/sh

How can I use bash variables in .tmux.conf?

I'm trying to write my own .tmux.config file and have managed to get most of it working (the file is quite simple). The intention is to have a minicom session running on the right side, whilst simultaneously saving the communication into a file. There is one problem. I define a UART_LOGFILE_NAME variable in my .bashrc file and export it, however, tmux still returns invalid environment variable. Is there something I'm missing?
my .tmux.conf file looks somewhat like this:
new-session -s serial -n console
split-window -h -l 30%
send-keys "sudo minicom rasp.serial -C ${UART_LOGFILE_NAME}"
split-window -v -l 10%
I can't find anything relevant in tmux manual entry except for the GLOBAL AND SESSION ENVIRONMENT. Also, sice I'm using the send-keys command I thought that the .bash_profile and therefore .bashrc files have been sourced before the minicom is launched (I have already managed to get the minicom running without logging and all panes have the UART_LOGFILE_NAME variable correctly defined after when I run tmux without logging of minicom session).
Thank you for your help

Show bash prompt after killing a process in tmux

If I run create a tmux session like this:
tmux new -d -s dev -n server -c ~/projects 'make run-server'
I now have a window running my make task. If I attach to that window and press ctrl+c, because I have remain-on-exit enabled, I will see "Pane is dead". :relaunch-pane will re-run make run-server, but what I really want when I press ctrl+c is to see the Bash prompt, allowing me to enter whatever command I want.
Is there any way to do this besides creating a generic window and typing the command manually each time I create it?
I was able to get this working by sending keys in a separate command:
tmux new -d -s dev -n server -c ~/projects
tmux send-keys -t dev:server 'make run-server' Enter

Script to start command in byobu tab

I have used screen to start a server process, to witch I can later attach if needed and deatach again to carry on with other things. It worked fine but I have found byobu recently and I really love it.
I want to use the same kind of scripts to run the server but instead of a screen sesson I would like to attach it to a byobu tab.
I'm using byobu-tmux (because it looks better). How could I do it?
My original scripts (they both do more, but these are the relevant parts):
# Startup
screen -a -dmS <name> <command>
# Attach
screen -x <name>
I'm using Ubuntu server 16.04
I don't know screen commands, so here is a quick sample commands for byobu:
To create new tabs (called screens) inside the current session you can:
byobu new-window "ls -la && sleep 5"
To start a new session (new byobu instance you can attach to) with a command you can:
byobu new-session -s "session name" "ls -la && sleep 5"
To create it already detached:
byobu new-session -d -s "session name" "ls -la && sleep 5"
To attach to a session by name:
byobu attach -t "session name"

Detached tmux session running command containing variables

Ok everyone, here is an issue which bugged me for quite some time now:
I am trying to run a bash script, which stores certain values in variables, and then starts another command in a detached session, so that the script can continue running because the command takes ages to finish. That's all fine and nice, but the problem is that the command which should be run in the detached session contains variables. This is not a problem when using screen as the "session manager" (or whatever you want to call a program doing session management):
read -p "Session name:" sessionname
read -p "Filename:" filename
screen -S "$sessionname" -d -m nano "$filename"
works fine, opening nano in the detached screen as expected. But when i re-attach to the session and close it, my terminal is cleared and sometinmes some of the text from the file is barfed right before my cursor. I realized that this is an issue with how screen works, so i decided to switch to tmux, but now a whole new realm of prblems opens:
tmux new-session -d -s "$sessionname" nano "$filename"
works fine on cygwin, but on my raspberry 2b running raspbian jessie it throws this error:
usage: new-session [-AdDp] [-c start-directory] [-F format] [-n window-name] [-s session-name] [-t tar
get-session] [-x with] [-y height] [command]
and trying tmux ls afterwards to see if anything started gives me this on the raspberry:
failed to connect to server: No such file or directory
while on cygwin, if no sessions are running, it gives me this:
no server running on /tmp/tmux-197609/default
How can it be that tmux on two different plattforms behaves conmpletely different? Have i installed it wrong on the raspberry (sudo apt-get install tmux) ?
I am all out of ideas what might be the problem here, so any help is greatly appreciated. (Sorry for confused spelling & grammar, non-native english speaker here)

Resources