How can I send commands to specific terminal windows?












13

















I'd like to write a script for opening multiple programs (servers) simultanously in separate terminals - doesn't matter which one - and assign different commands to different terminals with commands "landing" inside the correct terminal. Is this possible?

Maybe, something like this:




  1. open terminal1

  2. open terminal2 //simultanously with 1.

  3. command1 //execute in terminal1 without opening a new terminal window

  4. command2 //execute in terminal2 without opening a new terminal window

  5. ...


Can I somehow label terminal windows so commands are executed inside correct terminal?



I'd also like to watch all terminals while their programs are running - my programs have an argument for printing trace/debug to terminal. So I'd like to see what messages are exchanged between them.



NOTE: I'm less concerned about security of exchanged data since this script should serve as a "simulation". I've configured each server to run from an allocated port on localhost.










share|improve this question

























  • Check pssh ....

    – heemayl
    Jun 27 '15 at 17:29











  • How accurate should the timing be; is a marge of let's say 2 seconds (per terminal) appropriate?

    – Jacob Vlijm
    Jun 27 '15 at 17:34











  • @JacobVlijm: it's more important to me to assign commands correctly to according terminal "window"

    – Aliakbar Ahmadi
    Jun 27 '15 at 17:39






  • 1





    Can be done, especially when it is about simulation, will post back :)

    – Jacob Vlijm
    Jun 27 '15 at 17:43








  • 1





    @JacomVlijm: actually my question is incidently solved: for sending a command to its correct instance each command must be prefixed with the datadir that instance starts upon! But for my luck this is implemented in bitcoin but i'll just leave the question unanswered .. perhaps someone comes up with a more general idea for any program!? :) But thanks though!

    – Aliakbar Ahmadi
    Jun 27 '15 at 21:53
















13

















I'd like to write a script for opening multiple programs (servers) simultanously in separate terminals - doesn't matter which one - and assign different commands to different terminals with commands "landing" inside the correct terminal. Is this possible?

Maybe, something like this:




  1. open terminal1

  2. open terminal2 //simultanously with 1.

  3. command1 //execute in terminal1 without opening a new terminal window

  4. command2 //execute in terminal2 without opening a new terminal window

  5. ...


Can I somehow label terminal windows so commands are executed inside correct terminal?



I'd also like to watch all terminals while their programs are running - my programs have an argument for printing trace/debug to terminal. So I'd like to see what messages are exchanged between them.



NOTE: I'm less concerned about security of exchanged data since this script should serve as a "simulation". I've configured each server to run from an allocated port on localhost.










share|improve this question

























  • Check pssh ....

    – heemayl
    Jun 27 '15 at 17:29











  • How accurate should the timing be; is a marge of let's say 2 seconds (per terminal) appropriate?

    – Jacob Vlijm
    Jun 27 '15 at 17:34











  • @JacobVlijm: it's more important to me to assign commands correctly to according terminal "window"

    – Aliakbar Ahmadi
    Jun 27 '15 at 17:39






  • 1





    Can be done, especially when it is about simulation, will post back :)

    – Jacob Vlijm
    Jun 27 '15 at 17:43








  • 1





    @JacomVlijm: actually my question is incidently solved: for sending a command to its correct instance each command must be prefixed with the datadir that instance starts upon! But for my luck this is implemented in bitcoin but i'll just leave the question unanswered .. perhaps someone comes up with a more general idea for any program!? :) But thanks though!

    – Aliakbar Ahmadi
    Jun 27 '15 at 21:53














13












13








13


3








I'd like to write a script for opening multiple programs (servers) simultanously in separate terminals - doesn't matter which one - and assign different commands to different terminals with commands "landing" inside the correct terminal. Is this possible?

Maybe, something like this:




  1. open terminal1

  2. open terminal2 //simultanously with 1.

  3. command1 //execute in terminal1 without opening a new terminal window

  4. command2 //execute in terminal2 without opening a new terminal window

  5. ...


Can I somehow label terminal windows so commands are executed inside correct terminal?



I'd also like to watch all terminals while their programs are running - my programs have an argument for printing trace/debug to terminal. So I'd like to see what messages are exchanged between them.



NOTE: I'm less concerned about security of exchanged data since this script should serve as a "simulation". I've configured each server to run from an allocated port on localhost.










share|improve this question


















I'd like to write a script for opening multiple programs (servers) simultanously in separate terminals - doesn't matter which one - and assign different commands to different terminals with commands "landing" inside the correct terminal. Is this possible?

Maybe, something like this:




  1. open terminal1

  2. open terminal2 //simultanously with 1.

  3. command1 //execute in terminal1 without opening a new terminal window

  4. command2 //execute in terminal2 without opening a new terminal window

  5. ...


Can I somehow label terminal windows so commands are executed inside correct terminal?



I'd also like to watch all terminals while their programs are running - my programs have an argument for printing trace/debug to terminal. So I'd like to see what messages are exchanged between them.



NOTE: I'm less concerned about security of exchanged data since this script should serve as a "simulation". I've configured each server to run from an allocated port on localhost.







command-line bash scripts gnome-terminal






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 29 '15 at 5:44









Jacob Vlijm

63.7k9125219




63.7k9125219










asked Jun 27 '15 at 17:12









Aliakbar AhmadiAliakbar Ahmadi

2001210




2001210













  • Check pssh ....

    – heemayl
    Jun 27 '15 at 17:29











  • How accurate should the timing be; is a marge of let's say 2 seconds (per terminal) appropriate?

    – Jacob Vlijm
    Jun 27 '15 at 17:34











  • @JacobVlijm: it's more important to me to assign commands correctly to according terminal "window"

    – Aliakbar Ahmadi
    Jun 27 '15 at 17:39






  • 1





    Can be done, especially when it is about simulation, will post back :)

    – Jacob Vlijm
    Jun 27 '15 at 17:43








  • 1





    @JacomVlijm: actually my question is incidently solved: for sending a command to its correct instance each command must be prefixed with the datadir that instance starts upon! But for my luck this is implemented in bitcoin but i'll just leave the question unanswered .. perhaps someone comes up with a more general idea for any program!? :) But thanks though!

    – Aliakbar Ahmadi
    Jun 27 '15 at 21:53



















  • Check pssh ....

    – heemayl
    Jun 27 '15 at 17:29











  • How accurate should the timing be; is a marge of let's say 2 seconds (per terminal) appropriate?

    – Jacob Vlijm
    Jun 27 '15 at 17:34











  • @JacobVlijm: it's more important to me to assign commands correctly to according terminal "window"

    – Aliakbar Ahmadi
    Jun 27 '15 at 17:39






  • 1





    Can be done, especially when it is about simulation, will post back :)

    – Jacob Vlijm
    Jun 27 '15 at 17:43








  • 1





    @JacomVlijm: actually my question is incidently solved: for sending a command to its correct instance each command must be prefixed with the datadir that instance starts upon! But for my luck this is implemented in bitcoin but i'll just leave the question unanswered .. perhaps someone comes up with a more general idea for any program!? :) But thanks though!

    – Aliakbar Ahmadi
    Jun 27 '15 at 21:53

















Check pssh ....

– heemayl
Jun 27 '15 at 17:29





Check pssh ....

– heemayl
Jun 27 '15 at 17:29













How accurate should the timing be; is a marge of let's say 2 seconds (per terminal) appropriate?

– Jacob Vlijm
Jun 27 '15 at 17:34





How accurate should the timing be; is a marge of let's say 2 seconds (per terminal) appropriate?

– Jacob Vlijm
Jun 27 '15 at 17:34













@JacobVlijm: it's more important to me to assign commands correctly to according terminal "window"

– Aliakbar Ahmadi
Jun 27 '15 at 17:39





@JacobVlijm: it's more important to me to assign commands correctly to according terminal "window"

– Aliakbar Ahmadi
Jun 27 '15 at 17:39




1




1





Can be done, especially when it is about simulation, will post back :)

– Jacob Vlijm
Jun 27 '15 at 17:43







Can be done, especially when it is about simulation, will post back :)

– Jacob Vlijm
Jun 27 '15 at 17:43






1




1





@JacomVlijm: actually my question is incidently solved: for sending a command to its correct instance each command must be prefixed with the datadir that instance starts upon! But for my luck this is implemented in bitcoin but i'll just leave the question unanswered .. perhaps someone comes up with a more general idea for any program!? :) But thanks though!

– Aliakbar Ahmadi
Jun 27 '15 at 21:53





@JacomVlijm: actually my question is incidently solved: for sending a command to its correct instance each command must be prefixed with the datadir that instance starts upon! But for my luck this is implemented in bitcoin but i'll just leave the question unanswered .. perhaps someone comes up with a more general idea for any program!? :) But thanks though!

– Aliakbar Ahmadi
Jun 27 '15 at 21:53










1 Answer
1






active

oldest

votes


















14














Since you mention you solved the problem for your specific situation, below a solution for general purpose. Thanks to xdotool's --sync option, it works pretty reliable in the tests I ran; I could "send" commands to specific terminal windows and it ran perfectly without an exception.



How it works in practice



The solution exists from a script, which can be run with two options
-set and -run:





  1. To set up (open) an arbitrary number of terminal windows, in this example 3:



    target_term -set 3


    Three new terminals will open up, their window id is remembered in a hidden file:



    enter image description here



    For clarity reasons I minimized the terminal window I ran the command from :)




  2. Now that I created three windows, I can send commands to either one of them with the run command (e.g.):



    target_term -run 2 echo "Monkey eats banana since it ran out of peanuts"


    As shown below, the command ran in the second terminal:



    enter image description here



    Subsequently, I can send a command to the first terminal:



     target_term -run 1 sudo apt-get update


    making sudo apt-get update run in terminal 1:



    enter image description here



    and so on...




How to set up





  1. The script needs both wmctrl and xdotool:



    sudo apt-get install wmctrl xdotool


  2. Copy the script below into an empty file, safe it as target_term (no extension!) in ~/bin (create the directory ~/bin if necessary.



  3. Make the script executable (don't forget) and either log out/in or run:



    source ~/.profile



  4. Now setup your terminal windows, with the number of required windows as an argument:



    target_term -set <number_of_windows>



  5. Now you can "send" commands to either one of your terminals with the command:



    target_term -run <terminal_number> <command_to_run>



The script



#!/usr/bin/env python3
import subprocess
import os
import sys
import time
#--- set your terminal below
application = "gnome-terminal"
#---

option = sys.argv[1]
data = os.environ["HOME"]+"/.term_list"

def current_windows():
w_list = subprocess.check_output(["wmctrl", "-lp"]).decode("utf-8")
w_lines = [l for l in w_list.splitlines()]
try:
pid = subprocess.check_output(["pgrep", application]).decode("utf-8").strip()
return [l for l in w_lines if str(pid) in l]
except subprocess.CalledProcessError:
return

def arr_windows(n):
w_count1 = current_windows()
for requested in range(n):
subprocess.Popen([application])
called =
while len(called) < n:
time.sleep(1)
w_count2 = current_windows()
add = [w for w in w_count2 if not w in w_count1]
[called.append(w.split()[0]) for w in add if not w in called]
w_count1 = w_count2

return called

def run_intterm(w, command):
subprocess.call(["xdotool", "windowfocus", "--sync", w])
subprocess.call(["xdotool", "type", command+"n"])

if option == "-set":
open(data, "w").write("")
n = int(sys.argv[2])
new = arr_windows(n)
for w in new:
open(data, "a").write(w+"n")
elif option == "-run":
t_term = open(data).read().splitlines()[int(sys.argv[2])-1]
command = (" ").join(sys.argv[3:])
run_intterm(t_term, command)


Notes





  • The script is set for gnome-terminal, but can be used for any terminal (or other program as well) by changing the application in the head section of the script:



    #--- set your terminal below
    application = "gnome-terminal"
    #---


  • The commands above can (of course) be run from a script as well in case you'd lile to use it for some kind of a simulation.

  • The script waits until both the targeted window has focus and the command is done typing, so the command will always land in the right terminal window.


  • No need to say that the script only works with the terminal setup (windows) that was called by the command:



    target_term -set


    The terminal windows will then be "labelled" by the script, like you mention in your question.



  • In case you start a new target_term session, the hidden file, created by the script will simply be overwritten, so there is no need to remove it otherwise.






share|improve this answer

























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f641683%2fhow-can-i-send-commands-to-specific-terminal-windows%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    14














    Since you mention you solved the problem for your specific situation, below a solution for general purpose. Thanks to xdotool's --sync option, it works pretty reliable in the tests I ran; I could "send" commands to specific terminal windows and it ran perfectly without an exception.



    How it works in practice



    The solution exists from a script, which can be run with two options
    -set and -run:





    1. To set up (open) an arbitrary number of terminal windows, in this example 3:



      target_term -set 3


      Three new terminals will open up, their window id is remembered in a hidden file:



      enter image description here



      For clarity reasons I minimized the terminal window I ran the command from :)




    2. Now that I created three windows, I can send commands to either one of them with the run command (e.g.):



      target_term -run 2 echo "Monkey eats banana since it ran out of peanuts"


      As shown below, the command ran in the second terminal:



      enter image description here



      Subsequently, I can send a command to the first terminal:



       target_term -run 1 sudo apt-get update


      making sudo apt-get update run in terminal 1:



      enter image description here



      and so on...




    How to set up





    1. The script needs both wmctrl and xdotool:



      sudo apt-get install wmctrl xdotool


    2. Copy the script below into an empty file, safe it as target_term (no extension!) in ~/bin (create the directory ~/bin if necessary.



    3. Make the script executable (don't forget) and either log out/in or run:



      source ~/.profile



    4. Now setup your terminal windows, with the number of required windows as an argument:



      target_term -set <number_of_windows>



    5. Now you can "send" commands to either one of your terminals with the command:



      target_term -run <terminal_number> <command_to_run>



    The script



    #!/usr/bin/env python3
    import subprocess
    import os
    import sys
    import time
    #--- set your terminal below
    application = "gnome-terminal"
    #---

    option = sys.argv[1]
    data = os.environ["HOME"]+"/.term_list"

    def current_windows():
    w_list = subprocess.check_output(["wmctrl", "-lp"]).decode("utf-8")
    w_lines = [l for l in w_list.splitlines()]
    try:
    pid = subprocess.check_output(["pgrep", application]).decode("utf-8").strip()
    return [l for l in w_lines if str(pid) in l]
    except subprocess.CalledProcessError:
    return

    def arr_windows(n):
    w_count1 = current_windows()
    for requested in range(n):
    subprocess.Popen([application])
    called =
    while len(called) < n:
    time.sleep(1)
    w_count2 = current_windows()
    add = [w for w in w_count2 if not w in w_count1]
    [called.append(w.split()[0]) for w in add if not w in called]
    w_count1 = w_count2

    return called

    def run_intterm(w, command):
    subprocess.call(["xdotool", "windowfocus", "--sync", w])
    subprocess.call(["xdotool", "type", command+"n"])

    if option == "-set":
    open(data, "w").write("")
    n = int(sys.argv[2])
    new = arr_windows(n)
    for w in new:
    open(data, "a").write(w+"n")
    elif option == "-run":
    t_term = open(data).read().splitlines()[int(sys.argv[2])-1]
    command = (" ").join(sys.argv[3:])
    run_intterm(t_term, command)


    Notes





    • The script is set for gnome-terminal, but can be used for any terminal (or other program as well) by changing the application in the head section of the script:



      #--- set your terminal below
      application = "gnome-terminal"
      #---


    • The commands above can (of course) be run from a script as well in case you'd lile to use it for some kind of a simulation.

    • The script waits until both the targeted window has focus and the command is done typing, so the command will always land in the right terminal window.


    • No need to say that the script only works with the terminal setup (windows) that was called by the command:



      target_term -set


      The terminal windows will then be "labelled" by the script, like you mention in your question.



    • In case you start a new target_term session, the hidden file, created by the script will simply be overwritten, so there is no need to remove it otherwise.






    share|improve this answer






























      14














      Since you mention you solved the problem for your specific situation, below a solution for general purpose. Thanks to xdotool's --sync option, it works pretty reliable in the tests I ran; I could "send" commands to specific terminal windows and it ran perfectly without an exception.



      How it works in practice



      The solution exists from a script, which can be run with two options
      -set and -run:





      1. To set up (open) an arbitrary number of terminal windows, in this example 3:



        target_term -set 3


        Three new terminals will open up, their window id is remembered in a hidden file:



        enter image description here



        For clarity reasons I minimized the terminal window I ran the command from :)




      2. Now that I created three windows, I can send commands to either one of them with the run command (e.g.):



        target_term -run 2 echo "Monkey eats banana since it ran out of peanuts"


        As shown below, the command ran in the second terminal:



        enter image description here



        Subsequently, I can send a command to the first terminal:



         target_term -run 1 sudo apt-get update


        making sudo apt-get update run in terminal 1:



        enter image description here



        and so on...




      How to set up





      1. The script needs both wmctrl and xdotool:



        sudo apt-get install wmctrl xdotool


      2. Copy the script below into an empty file, safe it as target_term (no extension!) in ~/bin (create the directory ~/bin if necessary.



      3. Make the script executable (don't forget) and either log out/in or run:



        source ~/.profile



      4. Now setup your terminal windows, with the number of required windows as an argument:



        target_term -set <number_of_windows>



      5. Now you can "send" commands to either one of your terminals with the command:



        target_term -run <terminal_number> <command_to_run>



      The script



      #!/usr/bin/env python3
      import subprocess
      import os
      import sys
      import time
      #--- set your terminal below
      application = "gnome-terminal"
      #---

      option = sys.argv[1]
      data = os.environ["HOME"]+"/.term_list"

      def current_windows():
      w_list = subprocess.check_output(["wmctrl", "-lp"]).decode("utf-8")
      w_lines = [l for l in w_list.splitlines()]
      try:
      pid = subprocess.check_output(["pgrep", application]).decode("utf-8").strip()
      return [l for l in w_lines if str(pid) in l]
      except subprocess.CalledProcessError:
      return

      def arr_windows(n):
      w_count1 = current_windows()
      for requested in range(n):
      subprocess.Popen([application])
      called =
      while len(called) < n:
      time.sleep(1)
      w_count2 = current_windows()
      add = [w for w in w_count2 if not w in w_count1]
      [called.append(w.split()[0]) for w in add if not w in called]
      w_count1 = w_count2

      return called

      def run_intterm(w, command):
      subprocess.call(["xdotool", "windowfocus", "--sync", w])
      subprocess.call(["xdotool", "type", command+"n"])

      if option == "-set":
      open(data, "w").write("")
      n = int(sys.argv[2])
      new = arr_windows(n)
      for w in new:
      open(data, "a").write(w+"n")
      elif option == "-run":
      t_term = open(data).read().splitlines()[int(sys.argv[2])-1]
      command = (" ").join(sys.argv[3:])
      run_intterm(t_term, command)


      Notes





      • The script is set for gnome-terminal, but can be used for any terminal (or other program as well) by changing the application in the head section of the script:



        #--- set your terminal below
        application = "gnome-terminal"
        #---


      • The commands above can (of course) be run from a script as well in case you'd lile to use it for some kind of a simulation.

      • The script waits until both the targeted window has focus and the command is done typing, so the command will always land in the right terminal window.


      • No need to say that the script only works with the terminal setup (windows) that was called by the command:



        target_term -set


        The terminal windows will then be "labelled" by the script, like you mention in your question.



      • In case you start a new target_term session, the hidden file, created by the script will simply be overwritten, so there is no need to remove it otherwise.






      share|improve this answer




























        14












        14








        14







        Since you mention you solved the problem for your specific situation, below a solution for general purpose. Thanks to xdotool's --sync option, it works pretty reliable in the tests I ran; I could "send" commands to specific terminal windows and it ran perfectly without an exception.



        How it works in practice



        The solution exists from a script, which can be run with two options
        -set and -run:





        1. To set up (open) an arbitrary number of terminal windows, in this example 3:



          target_term -set 3


          Three new terminals will open up, their window id is remembered in a hidden file:



          enter image description here



          For clarity reasons I minimized the terminal window I ran the command from :)




        2. Now that I created three windows, I can send commands to either one of them with the run command (e.g.):



          target_term -run 2 echo "Monkey eats banana since it ran out of peanuts"


          As shown below, the command ran in the second terminal:



          enter image description here



          Subsequently, I can send a command to the first terminal:



           target_term -run 1 sudo apt-get update


          making sudo apt-get update run in terminal 1:



          enter image description here



          and so on...




        How to set up





        1. The script needs both wmctrl and xdotool:



          sudo apt-get install wmctrl xdotool


        2. Copy the script below into an empty file, safe it as target_term (no extension!) in ~/bin (create the directory ~/bin if necessary.



        3. Make the script executable (don't forget) and either log out/in or run:



          source ~/.profile



        4. Now setup your terminal windows, with the number of required windows as an argument:



          target_term -set <number_of_windows>



        5. Now you can "send" commands to either one of your terminals with the command:



          target_term -run <terminal_number> <command_to_run>



        The script



        #!/usr/bin/env python3
        import subprocess
        import os
        import sys
        import time
        #--- set your terminal below
        application = "gnome-terminal"
        #---

        option = sys.argv[1]
        data = os.environ["HOME"]+"/.term_list"

        def current_windows():
        w_list = subprocess.check_output(["wmctrl", "-lp"]).decode("utf-8")
        w_lines = [l for l in w_list.splitlines()]
        try:
        pid = subprocess.check_output(["pgrep", application]).decode("utf-8").strip()
        return [l for l in w_lines if str(pid) in l]
        except subprocess.CalledProcessError:
        return

        def arr_windows(n):
        w_count1 = current_windows()
        for requested in range(n):
        subprocess.Popen([application])
        called =
        while len(called) < n:
        time.sleep(1)
        w_count2 = current_windows()
        add = [w for w in w_count2 if not w in w_count1]
        [called.append(w.split()[0]) for w in add if not w in called]
        w_count1 = w_count2

        return called

        def run_intterm(w, command):
        subprocess.call(["xdotool", "windowfocus", "--sync", w])
        subprocess.call(["xdotool", "type", command+"n"])

        if option == "-set":
        open(data, "w").write("")
        n = int(sys.argv[2])
        new = arr_windows(n)
        for w in new:
        open(data, "a").write(w+"n")
        elif option == "-run":
        t_term = open(data).read().splitlines()[int(sys.argv[2])-1]
        command = (" ").join(sys.argv[3:])
        run_intterm(t_term, command)


        Notes





        • The script is set for gnome-terminal, but can be used for any terminal (or other program as well) by changing the application in the head section of the script:



          #--- set your terminal below
          application = "gnome-terminal"
          #---


        • The commands above can (of course) be run from a script as well in case you'd lile to use it for some kind of a simulation.

        • The script waits until both the targeted window has focus and the command is done typing, so the command will always land in the right terminal window.


        • No need to say that the script only works with the terminal setup (windows) that was called by the command:



          target_term -set


          The terminal windows will then be "labelled" by the script, like you mention in your question.



        • In case you start a new target_term session, the hidden file, created by the script will simply be overwritten, so there is no need to remove it otherwise.






        share|improve this answer















        Since you mention you solved the problem for your specific situation, below a solution for general purpose. Thanks to xdotool's --sync option, it works pretty reliable in the tests I ran; I could "send" commands to specific terminal windows and it ran perfectly without an exception.



        How it works in practice



        The solution exists from a script, which can be run with two options
        -set and -run:





        1. To set up (open) an arbitrary number of terminal windows, in this example 3:



          target_term -set 3


          Three new terminals will open up, their window id is remembered in a hidden file:



          enter image description here



          For clarity reasons I minimized the terminal window I ran the command from :)




        2. Now that I created three windows, I can send commands to either one of them with the run command (e.g.):



          target_term -run 2 echo "Monkey eats banana since it ran out of peanuts"


          As shown below, the command ran in the second terminal:



          enter image description here



          Subsequently, I can send a command to the first terminal:



           target_term -run 1 sudo apt-get update


          making sudo apt-get update run in terminal 1:



          enter image description here



          and so on...




        How to set up





        1. The script needs both wmctrl and xdotool:



          sudo apt-get install wmctrl xdotool


        2. Copy the script below into an empty file, safe it as target_term (no extension!) in ~/bin (create the directory ~/bin if necessary.



        3. Make the script executable (don't forget) and either log out/in or run:



          source ~/.profile



        4. Now setup your terminal windows, with the number of required windows as an argument:



          target_term -set <number_of_windows>



        5. Now you can "send" commands to either one of your terminals with the command:



          target_term -run <terminal_number> <command_to_run>



        The script



        #!/usr/bin/env python3
        import subprocess
        import os
        import sys
        import time
        #--- set your terminal below
        application = "gnome-terminal"
        #---

        option = sys.argv[1]
        data = os.environ["HOME"]+"/.term_list"

        def current_windows():
        w_list = subprocess.check_output(["wmctrl", "-lp"]).decode("utf-8")
        w_lines = [l for l in w_list.splitlines()]
        try:
        pid = subprocess.check_output(["pgrep", application]).decode("utf-8").strip()
        return [l for l in w_lines if str(pid) in l]
        except subprocess.CalledProcessError:
        return

        def arr_windows(n):
        w_count1 = current_windows()
        for requested in range(n):
        subprocess.Popen([application])
        called =
        while len(called) < n:
        time.sleep(1)
        w_count2 = current_windows()
        add = [w for w in w_count2 if not w in w_count1]
        [called.append(w.split()[0]) for w in add if not w in called]
        w_count1 = w_count2

        return called

        def run_intterm(w, command):
        subprocess.call(["xdotool", "windowfocus", "--sync", w])
        subprocess.call(["xdotool", "type", command+"n"])

        if option == "-set":
        open(data, "w").write("")
        n = int(sys.argv[2])
        new = arr_windows(n)
        for w in new:
        open(data, "a").write(w+"n")
        elif option == "-run":
        t_term = open(data).read().splitlines()[int(sys.argv[2])-1]
        command = (" ").join(sys.argv[3:])
        run_intterm(t_term, command)


        Notes





        • The script is set for gnome-terminal, but can be used for any terminal (or other program as well) by changing the application in the head section of the script:



          #--- set your terminal below
          application = "gnome-terminal"
          #---


        • The commands above can (of course) be run from a script as well in case you'd lile to use it for some kind of a simulation.

        • The script waits until both the targeted window has focus and the command is done typing, so the command will always land in the right terminal window.


        • No need to say that the script only works with the terminal setup (windows) that was called by the command:



          target_term -set


          The terminal windows will then be "labelled" by the script, like you mention in your question.



        • In case you start a new target_term session, the hidden file, created by the script will simply be overwritten, so there is no need to remove it otherwise.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jul 22 '17 at 20:22

























        answered Jun 28 '15 at 20:38









        Jacob VlijmJacob Vlijm

        63.7k9125219




        63.7k9125219






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Ask Ubuntu!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f641683%2fhow-can-i-send-commands-to-specific-terminal-windows%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            數位音樂下載

            When can things happen in Etherscan, such as the picture below?

            格利澤436b