Search code examples
bashgnu-screen

Automatically paste commands in multiple processes of a screen session


I have several bunch of linux commands contained in a text file, separated by \n\n, and I would like to automatically paste each in given screen processes. For the sake of clarity, let's say my command.txt simply contains :

#first bunch of commands:    
executable_script1.sh
mv executable_script1 directory1


#second bunch of commands:    
executable_script2.sh
mv executable_script2 directory2

So the first bunch of commands would run executable_script1.sh, after what it will move executable_script1. In this example, my screen contains 3 processes:

0$ htop
1$ bash
2$ bash

The name of the processes is irrelevant, the only important information is that I would like commands N in screen process N$, as 0$ is always a htop.

As for now, I have been copying/pasting manually each bunch of commands in the corresponding screen processes, which worked obviously, but now I will be dealing with more than 40 bunch of commands and as many screen processes. So, how could I paste the commands N to the N$ screen terminal automatically? I think a bash/shell script could do the trick, but I am not fluent enough with it. I currently use a python2 script to generate my command.txt file, so mind that I could create one txt file by bunch of commands pretty easily if needed.

Could you help me with this? Please feel free to ask for any missing information.

PS: I also asked this question on Unix Stackexchange, but this forum seems far less populated... If we find an answer here, I will invite the answerer to paste it under my Unix Stackexchange question as well, as this could help others!


Solution

  • I finally found my answer, thanks to this post! Sometimes it just takes some other keywords to find a solution, so I will answer this question in case some other fellows end up here.

    In a nutshell

    Automatically paste commands in the screen with the bash command :

    screen -x screen_name -p 1 -X stuff 'executable_script1.sh\n'
    

    where -p 1 refers to the 1$ screen process. Note that \n at the end of the command is necessary, as when you press enter after pasting a command line.

    Detailed steps

    1) Create the screen session you want to work in (here named 'screen_name') :

    screen -S screen_name
    

    with enough processes for all the commands (in my example, 0$ htop plus 2 processes : 1$ and 2$). Note that you can edit .screenrc in your home directory so that screen sessions start with a given number of processes by default. For this example, my .screenrc contains :

    screen -t htop
    screen -t 
    screen -t 
    

    2) Create bash files for every bunch of commands, to be executed by the different screen processes.

    Here I have 2 files, screen1 containing :

    #!/bin/bash
    
    screen -x screen_name -p 1 -X stuff 'executable_script1.sh\n'
    screen -x screen_name -p 1 -X stuff 'mv executable_script1 directory1\n'
    

    and screen2 containing :

    #!/bin/bash
    
    screen -x screen_name -p 2 -X stuff 'executable_script2.sh\n'
    screen -x screen_name -p 2 -X stuff 'mv executable_script2 directory2\n'
    

    3) Paste all your commands at once in a terminal, with :

    bash /path_to_screen1/screen1 & /path_to_screen2/screen2 &
    

    You can close this terminal immediately, even if you have long run calculations, as all it does is paste the command into screen. Manually open your screen session to verify that these lines are being executed.

    Needless to say, if you have a great number of commands to pass to many screen processes, you can create the bash files and paste the commands (steps 2 and 3) via a script (with pythonfor instance). Also executable_script1.sh can contain python calls if needed, with python python_script.py, as in a normal terminal.

    Hope this will help others!