4

What I want to do is really simple, but I can't seem to find a straightforward answer.

I found this question and this question, but they're not quite what I need - I'm not running multiple sessions or windows or anything crazy like that - I just want tmux to execute a command (likely a bash script) within itself, after it opens.

So basically I type "tmux", a session opens, and within the session tmux automatically executes "hello_world.sh", or something similar.

Does anyone know how to do this? The .tmux.conf file doesn't seem to take "send-keys", but I might be using it wrong.

Thank you all in advance.

Edit: Answered my own question thanks to JohnKiller. I didn't think about the fact that /root/.bashrc is run on log in, AND when TMUX opens. I added an if statement to it, answer below.

5

Thanks to JohnKiller's suggestion, I realized that .bashrc executes both when a new tty is opened, and when TMUX is opened in a terminal.

For future readers: The $TMUX variable is typically referenced to see if TMUX is running at all, but you could also use "pidof tmux". The $TMUX variable will be populated with something like: "/tmp/tmux-0/default,27389,0" if TMUX is running.

In my particular case, I'm running CentOS 6, and have it set to auto-login with root since it's just a test image. I was able to do that by editing /etc/init/tty.conf:

exec /bin/mingetty --autologin root $TTY

Now that it's set to auto-login, I added the following to /root/.bashrc:

if [[ `tty` == "/dev/tty1" ]] && [[ -z "$TMUX" ]];then
        tmux
fi

For newbies reading this, this says "If my terminal is terminal 1, and the $TMUX variable is zero-length, run tmux".

It's followed by:

if [[ -n "$TMUX" ]] && [[ ! -e "/root/.automatic_start_occurred" ]];then
        touch /root/.automatic_start_occurred
        /usr/bin/hello_world
fi

Again for newbies, this says "If $TMUX is non-zero in length, and the file ".automatic_start_occurred" does not exist (the "!" in the if statement), make the file "/root/.automatic_start_occurred" and then execute "hello_world" in /usr/bin.

This is exactly what I was looking for my system to do - After booting, TTY1 will pop up with TMUX, and the other TTYs will be left alone. When TMUX pops up for the first time, it will execute some arbitrary commands, and never do them again unless the file ".automatic_startup_occurred" is removed.

  • Great solution! Instead of needing to touch a new file (and remember to remove it, or to have it work across multiple terminals with tmux at once), I changed it to: if [[ -n "$TMUX" ]] && [[ -z $ALREADY_RAN_TMUX_STARTUP ]]; then export ALREADY_RAN_TMUX_STARTUP=true; /usr/bin/hello_world. YMMV; your answer got me what I needed, so thanks! – dwanderson Feb 16 '17 at 20:25
  • How can I use this to create a named session in tmux at startup? – Jim Pedid Mar 5 '18 at 15:25
  • 1
    @JimPedid you could add a line to rename the tmux session once it's active: after the "touch /root/.automatic_start_occurred" line, you could add tmux rename-session TestSession – Locane Mar 6 '18 at 23:04
0

https://wiki.archlinux.org/index.php/tmux#Autostart_tmux_with_default_tmux_layout

Edit ~/.bashrc and add:

if [ -z "$TMUX" ]; then
    YOUR CODE HERE
fi 
  • Hey JohnKiller - thanks for the suggestion, it made me realize the answer and I implemented it. You may want to edit your answer though, the "-z" Bash if test is for zero-length - your if statement says "If TMUX is not running, do this", but my question was about executing a command in TMUX after it begins running. – Locane Jan 12 '15 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.