'Open a new terminal tab and run a command inside, only after open tab

I'm trying to create a script to open a new tab at my rails project and run rails s inside this tab, to already start my server.

My command:

xfce4-terminal --tab --title="rails server" --working-directory="Documents/projects/rails_blog" --command="bash -c 'rails s';bash"

The new tab open at the correct directory.
But I always get this error:
bash: rails: command not found

I think that rails s is running before the new tab is opened

or --command tag expect only bash commands. I don't know

Why is that? And how can I solve this?

I already try all this answers below:
from superuser
stackoverflow - Open a new tab in gnome-terminal using command line
from stackexchange
from ask ubuntu - Open Terminal with multiple tabs and execute application
from ask ubuntu - How to open several Terminal Tabs in different folders (working directories)?



Solution 1:[1]

The following technique work well with newer gnome-terminal.

(1) This opens a new terminal window and executes "ls" and leaves the terminal window open

gnome-terminal --title=newWindow \\
               -- bash -c "ls; bash"

(2) This opens a new tab within current gnome-terminal and executes 'ls' in that tab.

gnome-terminal --tab --title=newTab \\
               -- bash -c "ls ;bash"

The above techniques work with GNOME Terminal 3.28.2 using VTE 0.52.2 +GNUTLS -PCRE2 Please note that old gnome-terminal options -e --command and -x are being deprecated in future releases.

The preferred way to execute a command after gnome-terminal is opened is to have the command to be executed after the last empty -- option. In the example above we execute bash command which in turn takes a set of commands that get executed within that shell. The trailing bash is needed in the first option otherwise the window/tab will close.

Hope this helps.

Solution 2:[2]

I use tmuxinator for this. You would put this in the director and then run tmuxinator start project and it starts the session in tmux for you.

# ~/.tmuxinator/project.yml

name: project
root: ~/projects/some_path

windows:
  - server: bundle && bundle exec rake db:migrate && rails s

Solution 3:[3]

I personally use iTerm2 for all my projects. And I run the below script which opens up multiple tabs in the same terminal window and executes the necessary commands.

#!/bin/bash

osascript -e 'tell application "iTerm2"
set newWindow to (create window with default profile)
tell current session of newWindow
    write text "echo Hello Terminal 1"
end tell 

tell current window
create tab with default profile
tell current session 
    write text "echo Hello Terminal 2"
end tell
end tell

tell current window
create tab with default profile
tell current session 
    write text "echo Hello Terminal 3"
end tell
end tell

end tell'

You can modify the command inside "" as needed.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ravi
Solution 2 Austio
Solution 3 Akella Niranjan