'Using a shell script to open new Ubuntu tab, then run commands within those new tabs

I'm using Windows Terminal running Ubuntu. My goal is to open 5 different tabs, navigate to specific directories within each of those tabs, and start certain services therein.

As a bonus, I'd like to colour the tabs and title them for tidiness' sake, but this is not vital.

#!/usr/bin/env sh

# simple demo script for launching all desired services at once
    
cd /main/directory
sudo service redis-server start
sudo service postgresql start
        
# Note this does not yet work as desired. The tabs are generated, but the command after the color value execute in the current tab, and not the newly created tab.
        
        
cmd.exe /c "wt.exe" --window 0 new-tab --title "APIService" --tabColor "#E74C3C" && cd apiserv/ && npm start
        
cmd.exe /c "wt.exe" --window 0 new-tab --title "AuthServ" --tabColor "#F39C12" && cd authserv/ && npm start
        
cmd.exe /c "wt.exe" --window 0 new-tab --title "MainServ" --tabColor "#F4D03F" && cd mainserv/ && rails s
        
cmd.exe /c "wt.exe" --window 0 new-tab --title "Sidekiq" --tabColor "#3498DB" && cd mainserv/ && sidekiq
    
cmd.exe /c "wt.exe" --window 0 new-tab --title "ClientServ" --tabColor "#27AE60" && cd clientserv/ && yarn start

I like to think I have most of (or at least some of) the pieces I need to achieve my desired result - there's no issue with getting the services running individually - but obviously I'm lacking some bash knowledge to get them working properly together. It'll open one tab, colour and (temporarily) name it, but the rest of the command (eg. '&& cd apiserv/ && npm start') executes in the original tab, and none of the other commands are reached.

At present, if I omit the commands after the colour value (at each occurrence, starting from the &&), the 5 tabs will be created and coloured. Their titles will appear, but quickly be overwritten by the default name. This isn't ideal, but it's not a main priority.



Solution 1:[1]

Following gives you the code structure you need :

#!/usr/bin/env sh

wt.exe -w -1 new-tab --title "APIService" --tabColor "#E74C3C" bash -c "cd /tmp\\; read -pEnter" \;\
             new-tab --title "AuthServ"   --tabColor "#F39C12" bash -c "cd /tmp\\; read -pEnter"
# ...

I don't have wsl to test at the moment (so you may need some adaptation), but it worked in git-bash.

You might need to change bash -c to wsl -e, or wsl -e bash -c

Solution 2:[2]

You may use command substitution:

Kubectl delete $(kubectl get pod -l app=foo -o name)

Solution 3:[3]

kubectl get -o name will write out resource names in kind/name format, one to a line. You can use this in combination with tools like xargs(1) to run pipelines like you suggest.

kubectl get job -l app=foo -o name | xargs kubectl delete

# help we're using the long-format label names and I don't remember
# what goes after `kubectl get -l`
kubectl get job -o name | grep foo | xargs kubectl delete

For the very specific command you show, I've often found it easier to use kubectl rollout restart to trigger a Deployment's redeployment sequence without actually making any changes. This will delete all of the pods managed by the Deployment, but only after creating new pods first; so you get the effect of restarting misbehaving Pods but without actually taking the whole application down.

kubectl rollout restart deployment/foo

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 Philippe
Solution 2
Solution 3 David Maze