Search code examples
linuxbashsshscreen

How to make a shell script that connects to a server and executes commands inside `screens`?


On Linux. I need to write a file containing a script.
My script should connect to a server, create a screen and execute a command. Several times automatically.

Pseudocode of the script:

ssh machine1
cd project_folder
screen -S screenA
python run.py -x
exit
screen -S screenB
python run.py -y
exit
exit

ssh machine2
cd project_folder
screen -S screenA
python run.py -z
exit
screen -S screenB
python run.py -t
exit
exit

ATM I can just connect and run each command manually.


Solution

  • Yes, thanks https://stackoverflow.com/users/9215267/siddharth-dushantha
    And for the part of screen the code is

    screen -dmS screen0
    screen -ls
    screen -r screen0 -p 0 -X stuff "the_command1^M"
    screen -r screen0 -p 0 -X stuff "the_command2^M"
    

    where ^M types enter at the end of the line.