How to run a second command in same screen session
I'm starting the first command in screen like this:
screen -d -m -S testen -t lalala watch df -h
which gives me a screen session running in the background and I can reconnect at a later time.
How can I run a second command in a new window in the same screen session?
Solution 1:
Start a named screen session (-S
) with a named window (-t
) adapting to the terminal size (-A
) in detached mode (-d -m
):
screen -S mySessionName -t myWinName0 -A -d -m
Start another named window (-t
) in the same screen session (-S
):
screen -S mySessionName -X screen -t myWinName2
Stuff a few commands (-X stuff $'cmds'
) into the first named window (-p
) in the session (-S
):
screen -S mySessionName -p myWinName0 -X stuff $'echo myWinName0\necho cmd1\necho cmd2\n'
Stuff a few commands (-X stuff $'cmds'
) into the second named window (-p
) in the session (-S
):
screen -S mySessionName -p myWinName1 -X stuff $'echo myWinName1\necho cmd1\necho cmd2\n'
List the screen sessions and reattach to see what happened:
screen -ls
screen -r mySessionName
Note: The linefeed (\n
) simulates pressing Enter. You could use semicolons to separate commands as well.
Solution 2:
Tried above approach, but the second command didn't execute on the second window. I slightly adjusted the example by using the window number, instead of the window name:
-
create session
screen -S mySessionName -t 0 -A -d -m
-
create second window
screen -S mySessionName -X screen -t 1
-
send command to first window
screen -S mySessionName -p 0 -X stuff $'echo myWinName0\necho cmd1\necho cmd2\n'
-
send command to second window
screen -S mySessionName -p 1 -X stuff $'echo myWinName0\necho cmd1\necho cmd2\n'
-
now check if the command has been runned on the first window
screen -R mySessionName -p 0
-
now check if the command has been runned on the second window
screen -R mySessionName -p 1
Tested with Screen 4.00.03 (CENTOS 6.3 x64).