Linux – How to run a second command in same screen session

gnu-screenlinux

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?

Best Answer

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.

Related Question