Tell me more ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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! : )

share|improve this question

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

4 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"
share|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

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.

share|improve this answer

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:

  1. create session

    screen -S mySessionName -t 0 -A -d -m
    
  2. create second window

    screen -S mySessionName -X screen -t 1
    
  3. send command to first window

    screen -S mySessionName -p 0 -X stuff $'echo myWinName0\necho cmd1\necho cmd2\n'
    
  4. send command to second window

    screen -S mySessionName -p 1 -X stuff $'echo myWinName0\necho cmd1\necho cmd2\n'
    
  5. now check if the command has been runned on the first window

    screen -R mySessionName -p 0
    
  6. 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).

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.