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'
Create a file named
my_script.sh
and give it the above contents.Make the file executable by running:
chmod 755 my_script.sh
orchmod +x my_script.sh
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
Are the Date, Time, and Datetime Classes Necessary
How to Get the Line of Code That Triggers a Query
How to Avoid Putting the Magic Encoding Comment on Top of Every Utf-8 File in Ruby 1.9
Write CSV in Ruby 1.9 and CSV::Writer
How to Share the Factories That I Have in a Gem and Use It in Other Project
Visit_Psych_Nodes_Alias: Unknown Alias: Default (Psych::Badalias)
Factorygirl Association Model Trouble: "Systemstackerror: Stack Level Too Deep"
Why Bundle Install Is Installing Gems in Vendor/Bundle
Aws Elastic Beanstalk - How to Upgrade Existing Environment from Ruby 2.1 to Ruby 2.2
Error to Installing SASS with Gem in Windows 8.1
Cannot Load Such File -- Readline (Loaderror) When Running Heroku Create --Stack Cedar
Sprockets::Circulardependencyerror in Store#Index
Error While Trying to Load the Gem 'Devise. Activesupport: Duration Can't Be Coerced into Integer
Shorten Long Numbers to Include K/M/B/T
How to Change "Devise: Password Reset Instruction Email's Subject"