13

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?

4 Answers 4

13

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.

1
  • You don't have to do the "stuff" bits. screen -S sessionName -t windowName1 -Adm <command> && screen -S sessionName -X screen -t windowName2 -Adm <command2>
    – Lotus
    Jul 6, 2016 at 15:45
6

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).

4

Inside screen:

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

Outside:

screen -X testen "screen newcmd"
4
  • 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, 2011 at 13:34
  • 1
    @bob: See example #3, "Outside screen", in this answer.
    – user1686
    May 5, 2011 at 13:50
  • hi grawity, screen -S testen -X 'screen echo "hi"' doesn't work.
    – user79879
    May 5, 2011 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" Aug 19, 2011 at 22:58
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy