Command Line Script to Run Multiple Commands in Different Tabs

Multiple Log Tails in Different Command Line Tabs Image

Why Use a Bash Script that Launches Multiple Command Line Tabs?

Imagine you just rebooted your computer now for some reason…

Do you have this feeling of “Ok… Ii just need to open again a bunch command line tabs, run a series of scripts, start the log tails, open the IDE and I will be good to go!”?

If you still find yourself having to run all these commands, launching all the Docker containers and mounting the entire setup when you reboot your Linux machine, then keep reading! The solution may be a few lines of bash script 😉

How to Run Multiple Commands in Different Command Line Tabs

After struggling with this for some time, I decided to create a simple bash script, that I could run on the system boot, to take care of mounting all this setup for me.

Creating the script to run multiple commands isn’t really a hard task. I wrote some articles before about this here and here.

The tricky part was that I wanted some of those commands to run in different tabs so I can easily access the outputs for debugging purposes (like Docker containers, log monitoring, etc…).

I also didn’t want to go to the trouble of installing and configuring tools like tmux or tmuxifier (if you want something more advanced, definitely check them out!) for such a simple task like launching a few command line tabs. So, I decided to do some research and I found that the solution is rather simple if your Linux machine has gnome-terminal installed.

Here is an example:

#!/bin/bash

# Array of commands to run in different tabs
commands=(
    'cd ~/path/to/script1 && ./script1.sh'
    'cd ~/path/to/script2 && ./script2.sh'
    'cd ~/path/to/script3 && ./script3.sh'
)

# Build final command with all the tabs to launch
set finalCommand=""
for (( i = 0; i < ${#commands[@]} ; i++ )); do
    export finalCommand+="--tab -e 'bash -c \"${commands[$i]}\"' "
done

# Run the final command
eval "gnome-terminal "$finalCommand

The Multiple Log Monitoring Example

Another example situation in which I use this bash script is to launch tabs with all my debug logs open for quick access during my development. As a Back-End Engineer, I find this to be a must in my everyday work.

#!/bin/bash

# Array of commands to run in different tabs
commands=(
    'tail -f /var/log/apache2/access.log'
    'tail -f /var/log/apache2/error.log'
    'tail -f /usr/local/var/postgres/server.log'
)

# Build final command with all the tabs to launch
set finalCommand=""
for (( i = 0; i < ${#commands[@]}; i++ )); do
    export finalCommand+="--tab -e 'bash -c \"${commands[$i]}\"' "
done

# Run the final command
eval "gnome-terminal "$finalCommand

Here is the Gist link if you want to fork the script.

If you found this post helpful or have something to say, leave a comment bellow 😉