How to Write a Shell Script That Starts Tmux Session, and Then Runs a Ruby Script

How to write a shell script that starts tmux session, and then runs a ruby script


#!/bin/bash
tmux new-session -d -s my_session 'ruby run.rb'
  1. Create a file named my_script.sh and give it the above contents.

  2. Make the file executable by running:

    chmod 755 my_script.sh
    or
    chmod +x my_script.sh

  3. Then run the shell script:

    ./my_script.sh

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 rwxr-xr-x.

You can alternatively use chmod +x file_name on a file to make it executable.

Starting a new tmux session and detaching it, all inside a shell script

Start a shell, and send vagrant up to it, so you can see the errors.

tmux new-session -d -s rbt123
tmux send-keys 'vagrant up' C-m
tmux detach -s rtb123

The C-m means hit return.

Bash scripts with tmux to launch a 4-paned window

As others have mentioned, your commands are being run by the shell script before launching your $SHELL; there is no general way the instance of $SHELL can know what its parent ran before starting it.

To get the “initial command” into the shell history, you need to feed the command keystrokes directly to the instance of $SHELL itself (after it has been started, of course). In other contexts I might suggest using a small Expect program to spawn an instance of $SHELL, feed it the keystrokes, then use interact to tie the tty to the expect-spawned $SHELL.

But in the context of tmux, we can just use send-keys:

#!/bin/sh

tmux new-session -d -s foo 'exec pfoo'
tmux send-keys 'bundle exec thin start' 'C-m'
tmux rename-window 'Foo'
tmux select-window -t foo:0
tmux split-window -h 'exec pfoo'
tmux send-keys 'bundle exec compass watch' 'C-m'
tmux split-window -v -t 0 'exec pfoo'
tmux send-keys 'rake ts:start' 'C-m'
tmux split-window -v -t 1 'exec pfoo'
tmux -2 attach-session -t foo

How can I trigger a shell script and run in background (async) in Ruby?

@TanzeebKhalili's answer works, but you might consider Kernel.spawn(), which doesn't wait for the process to return:

pid = spawn("./test.sh")
Process.detach(pid)

Note that, according to the documentation, whether you use spawn() or manually fork() and system(), you should grab the PID and either Process.detach() or Process.wait() before exiting.

Regarding redirecting standard error and output, that's easy with spawn():

pid = spawn("./test.sh", :out => "test.out", :err => "test.err")
Process.detach(pid)

How to run a ruby shell script that shows some information, then asks for input and goes background?

Here's a simple example:

def do_stuff(input)
puts "Input received: #{input}"
sleep 10
end

print "Enter input value: "
input = gets
pid = fork { do_stuff(input) }
puts "test.rb started, pid: #{pid}"
Process.detach pid

Note the use of fork to start a new process, and detach to allow the subprocess to continue running while the main process exits.

Some platforms (Windows) don't support fork.



Related Topics



Leave a reply



Submit