I'm starting the first command in screen like this: screen -d -m -S testen -t lalala watch df -h

Now I have a screen session running in background and I can reconnect at a later time.

But how can I run a second command (in a new window) in the same screen session?

For any help! : )

link|improve this question
feedback

migrated from stackoverflow.com May 5 '11 at 13:29

This question came from our site for professional and enthusiast programmers.

3 Answers

Inside screen:

  • Ctrl-A, c to open a new shell window
  • Ctrl-A, :screen newcmd to run newcmd

Outside:

screen -X testen "screen newcmd"
link|improve this answer
hi grawity, i know this, but i would like to do it from an init script. basically i want to start 2 commands in seperate windows in the same screen session. see you – user79879 May 5 '11 at 13:34
1  
@bob: See example #3, "Outside screen", in this answer. – grawity May 5 '11 at 13:50
hi grawity, screen -S testen -X 'screen echo "hi"' doesn't work. – user79879 May 5 '11 at 13:59
I was also unable to get the -X method to work. Fwiw, my screen -v says "Screen version 4.00.03 (FAU) 23-Oct-06" – Lauri Lehtinen Aug 19 '11 at 22:58
feedback

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 1st 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 2nd 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.

link|improve this answer
feedback

You can setup an alternate .screenrc via the -c argument. In this new screenrc you can setup multiple commands to start when the screen session is initialized.

At the end of this alternate .screenrc put the following commands:

screen 1 cmd1 args
screen 2 cmd2 args

You don't need to setup an alternate configuration file if you only plan to run one type of screen session and the default commands run are always fine.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown