0

I want to run a specific command in a specific instance of the terminal window.

Example: - I open multiple instance of terminal window, say 2: A and B - Now I want to run shutdown command on terminal A from a new terminal C, without affecting terminal B.

2
  • 1
    Terminals can run commands typically with -c or --exec flag, but otherwise they run shell which takes input from keyboard, not anywhere else. You can simulate sending keypresses to a specific window, or you can run a "server" command that listens to commands, but otherwise - you can't just tell terminal A to do something from terminal B simply because that's how they're designed Sep 27, 2018 at 1:31
  • By, “run on terminal A from a new terminal C”, do you mean “run in C, but show the output in A”? I wrote a long answer about that here: askubuntu.com/a/954055/507051
    – dessert
    Sep 27, 2018 at 6:42

1 Answer 1

0

There is a command line tool called xdotool that allows you to mimic key-presses, commands, etc. using the process ID, process type, class, or name.

I was able to differentiate the different terminal instances by PID, and even rename them. So instead of having your current user and directory at the top of the window, it would have the unique name you set (e.g A, B, and C). And xdotool uses this as an identifier. Can be easier than remembering the ~10 digit PID.

So in terminal A, you can tell xdotool to mimic the key-presses for a certain command, and have them execute in terminal C. You should be able to put these commands in scripts, as well.

Here is the project site

Tested out some scripts, works pretty well. I was able to have A tell B to run a local server. In the same script, I used xdotool’s delay function so that B tells C to run a script to end the server process in B. So it seems like you can get pretty complex with it.

The only annoyance is that the standard terminal doesn’t receive the commands or key presses unless it’s the active window. Just one extra command works around this fine, but not ideal. Xterm has a setting that will let it receive commands when not the active window.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .