Score:4

Script to run a command in a terminal and then open a new terminal tab and run another command

co flag

I am running Ubuntu 21.04 and I want to create a script, that would do the following:

  1. Run:

    cd && cd path/to/repo && git pull && npm i && code . && npm run dev
    
  2. Then open a new GNOME Terminal tab using:

    gnome-terminal --tab
    
  3. Then switch to this tab and run:

    cd && cd path/to/repo && git reset --hard && git pull -f && npm i && npm run dev
    

So basically I want one terminal with 2 tabs running 2 development servers.

I tried with xdotool and xte, but it never worked (keys aren't even pressed).

Is it even possible to do what I want?

As suggested below I tried, it gave me failed to run cd so I googled and came up with the following:

gnome-terminal --tab -- /bin/bash -e -c "cd path/to/repo && ls && git pull && npm i && code . && npm run dev" --tab -- /bin/bash -e -c "cd path/to/repo && git reset --hard && git pull -f && npm i && npm run dev"

But the problem is that npm run dev is not supposed to ever stop, and so the second terminal tab is not ever opened. And I need to run them together...

vanadium avatar
cn flag
What did you try thus far? Running two sequences of commands from the command line of `gnome-terminal` may not anymore be possible, because any commands need to come at the end nowadays after `--`. You may need another terminal emulator to be able to automatically set up two tabbed sessions.
terdon avatar
cn flag
Minor note: you don't need `cd && cd path/to/repo`. Either the `path/to/repo` is a full path so you just do `cd path/to/repo` or, if it is relative to your `$HOME`, you can do `cd ~/path/to/repo`.
co flag
yeah I know about `cd && cd path/to/repo`, I am currently copying and pasting the command, so it's sort of my way of `git pull && npm i && npm run dev`, regardless the current directory.
Score:4
cn flag

Although gnome-terminal indicates that the -e option is deprecated, it still works in 3.40.3.

# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.

Developers seem to forget that this is the only way to do what you want, i.e., automatically set up different working environments in tabs. The "new" syntax, that adds commands to run at the end of the options after -- does not allow to do that. Once the -e option is removed, you will need to move to another terminal emulator for something like this.

So this, for example, still works thus far:

gnome-terminal --tab -e "htop" --tab -e "top"

Replace commands htop and top by your custom scripts.

co flag
Seems about to work, but it fires an error: ```# Error: Failed to execute child process “cd”: Failed to execve: No such file or directory # Error: Failed to execute child process “cd”: Failed to execve: No such file or directory``` but it does work with `gnome-terminal --tab -e "ping goo.gl"` why can't it run `cd` in there?...
Score:0
co flag

so the content of sh script is:

gnome-terminal --tab --title='frontend' -e "bash -c 'cd path/to/repo && git pull && npm i && code . && npm run dev'";
gnome-terminal --tab --title='backend' -e "bash -c 'cd path/to/repo && git reset --hard && git pull -f && npm i && npm run dev'"

Thanks @vanadium for helping to find out the direction of googling ;)

it does work as intended. If I open a terminal and write sh myScript.sh it'll open 2 extra tabs with backend and frontend repos running.

A little problem is that if I execute the sh file by double clicking on it it will open 2 terminal windows (ugly, I want tabs).

Don't really know how to fix this.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.