How to write a shell script that starts tmux session, and then runs a ruby script
I want to write a shell script that does this:
- First, create a tmux session
- Second, run a ruby script called "run.rb" INSIDE the tmux session
In pseudo-code, what I want to do:
tmux new -s my_session ruby run.rb # NOTE: I want this to run inside the my_session tmux session. tmux detach
How do I do this? (More posts I read, more confusing it gets.)
#!/bin/bash tmux new-session -d -s my_session 'ruby run.rb'
Create a file named
my_script.shand give it the above contents.
Make the file executable by running:
chmod 755 my_script.shor
chmod +x my_script.sh
Then run the shell script:
Making the shell script executable
When you perform the
chmod 755 filename command you allow everyone to read and execute the file, and the file owner is allowed to write to the file as well. You may need this for Perl and other scripts that should be run via a webserver. If you apply 755 to a directory, it means that everyone can go to it and get its file listing.
These permissions are usually translated into textual representation of
You can alternatively use
chmod +x file_name on a file to make it executable.
Write a shell script that starts tmux session and then runs a ruby , tmux new-session -d -s "myTempSession" /opt/my_script.sh. This starts a new detached session, named "myTempSession", executing your script. You can later Also see https://serverfault.com/questions/339390/run-command-in-detached- tmux-session for examples on using the send-keys command on a detached pane. #!/bin/bash tmux new-session -d -s my_session 'ruby run.rb' Create a file named my_script.sh and give it the above contents. Make the file executable by running: chmod 755 my_script.sh or chmod +x my_script.sh. Then run the shell script:./my_script.sh. Making the shell script executable
K M Rakibul Islam's updated code contains an unnecessary detach command at the end which causes an error message "no client found" (my_session has already been detached and thus is not in scope so tmux cannot understand which session you want to detach). The correct code should be:
#!/bin/bash tmux new-session -d -s my_session 'ruby run.rb'
How to execute code in a new tmux session, I want to write a shell script that does this: First, create a tmux session Second, run a ruby script called "run.rb" INSIDE the tmux session In pseudo-code, what I� #!/bin/bash tmux new -s my_session ruby run.rb tmux detach Créer un fichier nommé my_script.sh et donnez-lui le contenu ci-dessus. Rendre le fichier exécutable en cours d'exécution: chmod 755 my_script.sh ou chmod +x my_script.sh. puis lancez le script shell:./my_script.sh. Faire le script shell exécutable
With some experimenting, I figured out how to control tmux via shell script.
tmux new-session -d -s htop-session 'htop'; # start new detached tmux session, run htop tmux split-window; # split the detached tmux session tmux send 'htop -t' ENTER; # send 2nd command 'htop -t' to 2nd pane. I believe there's a `--target` option to target specific pane. tmux a; # open (attach) tmux session.
The above splits the tmux session into two window, and runs
htop in both.
To answer original question, you can run a ruby script and not detached the tmux session with command below:
tmux new-session -s ruby_session 'ruby run.rb'; # open tmux session and run ruby script.
How to write a shell script that starts tmux session, and then runs a , Since I've been writing more Go, I've been using a lot more plain text editors such Many people recommend the tmuxinator Ruby gem to configure tmux so I'm going to use raw bash scripts for launching tmux sessions. to start monitoring a specific log file; Attach to the session, viewing the first window. This script starts a session with the name "e" and three windows #!/bin/sh tmux new-session -s e -n etc -d 'cd /etc; bash -i' tmux new-window -t e:1 -n home 'cd ~; bash -i' tmux new-window -t e:2 -n log 'cd /var/log; bash -i' tmux select-window -t e:1 tmux -2 attach-session -t e
You could use teamocil to do this easily. You could just create a YAML file:
windows: - name: rubysession root: ~ layout: tiled panes: - ruby run.rb; tmux detach
If you named it 'rubysession.yml' then run:
And that would work perfectly for your purpose and require no hacks. Also teamocil is awesome for loads of other uses!
A basic tmux bash script explained � coleman.codes, Use the wemux Shell Script to share the same tmux sessions. You don't want to run that when you are already attached, so I check to make sure the TERM� You are running the command and then entering the interactive shell; the command run from the script, not being in an interactive shell, doesn't get recorded in the history. You really want a way to stuff (that's a technical term :) once upon a time it was TIOCSTI for "terminal ioctl(): stuff input") input for the shell into the window.
If you want to keep your tmux session alive after starting some commands, a possible solution is to start a bash with an init file:
tmux new -d -s mysession "bash --init-file foo.script"
where foo.script would contain your commands. Alternatively, you can feed the command to the shell directly from the command line:
tmux new -d -s mysession2 "bash --init-file <(echo ruby run.rb)"
Note that --init-file was meant for reading system wide initialization files like /etc/bash.bashrc so you might want to 'source' these in your script.
TMUX: My Getting Started Guide, Tmux login session script. Here's the idea: when you log in to your computer you always have to start the web browser, email, etc. So you create a script that starts them all at once. The script below is an extension of that concept. It creates a tmux session (tmux is like screen but better) and runs each program in its own window.
If this is your first time writing a script, don’t worry — shell scripting is not that complicated. That is, you can do some complicated things with shell scripts, but you can get there over time. If you know how to run commands at the command line, you can learn to write simple scripts in just 10 minutes.
In one terminal create a new session for screen, where foobar is the name of your screen session: screen -S foobar. Then in the other terminal, attach to that session. screen -x foobar. That's it, there were just two steps. Basic sharing with tmux. Again, there are only two steps. In the first terminal, start tmux where shared is the session name:
As the questioner wrote in a comment he has solved the problem: If I change the shell script to: #!/bin/bash tmux new-session -d -s dropx 'python /home/pi/drop/dropx.py' then the script works if called from the terminal, it opens a new tmux session and runs the python program inside, however, when I use a Systemd service to start the same shell script it fails to run.
- Could you post what you've tried, and how it confuses you?
- I don't know what to try.
- @hackstar15 Did my answer solve your problem? Please let me know!
- Do you want the ruby script to run inside the
tmuxsession? If so, K M Rakibul Islam's solution is not what you want.
- @chepner you're right! I want to run the run.rb script INSIDE the tmux session. how do i do this?
- Alternatively he can change the file permission with "chmod +x my_script.sh", with "+x" meaning "give(+) eXecutable rights".
- When the new session is created, the
bashscript is on hold, because the first
tmuxcommand doesn't exit while you are still attached to the new session.
- @KMRakibulIslam sorry, i need to run.rb to run INSIDE the tmux session. your solution runs it in the original terminal. Do you know how to run the run.rb script inside the tmux?
- @KMRakibulIslam Perfect!
- This is not working for me. Gives me session not found error: $cat tmux_sh.sh #!/bin/bash echo "step 1" tmux new-session -d -s rtb123 'vagrant up' echo "step 2" tmux detach -s rtb123 ./tmux_sh.sh step 1 step 2 session not found: rtb123
- How do you execute multiple commands instead of just one like
- @Muddz you could for example run
first command && second command && and so onor you could put your commands in a shell script and run it instead of the commands: