Score:3

Is it possible to close a terminal tab from the command line by targeting its title?

jp flag

For my development environment, I use a script to run, among others, a command to launch a terminal tab that runs an aliased command:

gnome-terminal --tab-with-profile="custom" --title="imageproc" -- bash -ic "imgp ss"

I'd like to be able to target and close the tab if I switch to a different project, but I'm coming up blank after googling and looking through several man pages.

Score:1
jp flag

Not exactly what I was looking for, but found a roundabout way to accomplish what I needed, in case this helps someone else:

The terminal tab in my case is a node server that outputs a log while it's running, so it stays open while the process is live. At least on Ubuntu, you can set the window to close itself based on the terminal profile preferences:

preferences window screenshot

Which means I can simply kill the process and the tab will close. To accomplish this, I added a command that I prepend to the scripts I use when switching projects:

ps -aux | grep "commandnamehere" | grep -v "grep" | awk '{print $2}' | xargs kill

Alternatively, you can kill the process occupying a specific port, which also worked in my case:

kill $(lsof -t -i:7777)

Edit:

In case you want to just slap the kill process before some other scripts like I did, you might also want a function that checks to see if there is a process to kill:

killpid() {
     PID=`ps -aux | grep "commandnamehere" | grep -v "grep" | awk '{print $2}'`;
     if [ ! -z "$PID" ]
     then
         echo "killing commandnamehere - $PID";
         echo $PID | xargs kill;
     fi
 }
hr flag
You can probably simplify the `ps | ... | kill` pipeline down to something like `pkill "commandnamehere"` or `pkill -f "commandnamehere"`
jp flag
@steeldriver I started with that, but in my specific case the command is another bash script, so it was easier to grep a unique identifier to get the process.
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.