28

when starts session named any name like this screen -S name1

i want to open tabs windows in this screen session like when open tabs in gnome-terminal like this

gnome-terminal --tab -e "some commands"

so how to do this ?

  • BOTH ı want to know how to open tabs sımılar to gnome termınal and ı want to execute some command ın ıt after ı open tab ın run tıme – Ahmed Zain El Dein May 5 '13 at 20:18
  • So the technique I described below shows the tabs that you're looking for then? Just need a way to run a command in them? – slm May 5 '13 at 20:21
  • but actually ı am begınner and ı dont understand what u want me to do exactly ı thought that there ıs syntax to open tabs ın the screen aın wındow but ıt seems that ı wıll add some lınes to some fıle – Ahmed Zain El Dein May 5 '13 at 20:29
  • ok i get that u want me to copy this and past in this file on my home but what i am asking about is screen -t name gives a title to the screen as i know and also i want to open those tabs in a named session for instance screen -S name1 --tab -e "some commands" i hope u get what i am asking about ? – Ahmed Zain El Dein May 5 '13 at 21:25
  • mmmm those are a pre defined tabs with a certain number this is not what i am asking sir :) i dont know how many tabs i need in the run time what i want is something similar to the gnome terminal command gnome-terminal --tab -e "commands " --tab -e " commands this will open one window of terminal with two tabs for instance i mean :) thank u – Ahmed Zain El Dein May 5 '13 at 21:39
66

1. Tabs in screen

You're looking for this to add to your .screenrc file:

screen -t tab1
screen -t tab2

Here's a nice basic .screenrc to get you started with a status bar etc. NOTE: This is typically located in your home directory /home/<username>/.screenrc.

screen -t validate #rtorrent
screen -t compile #irssi
screen -t bash3
screen -t bash4
screen -t bash5
altscreen on
term screen-256color
bind ',' prev
bind '.' next
#
#change the hardstatus settings to give an window list at the bottom of the
#screen, with the time and date and with the current window highlighted
hardstatus alwayslastline
#hardstatus string '%{= kG}%-Lw%{= kW}%50> %n%f* %t%{= kG}%+Lw%< %{= kG}%-=%c:%s%{-}'
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'

screenshot

ss of screen session

2. Tabs in screen (with commands run inside)

The example .screenrc below will create 2 tabs and run 3 echo commands in each.

screen -t tab1                                                                                     
select 0                                                                                           
stuff "echo 'tab1 cmd1'; echo 'tab1 cmd2'; echo 'tab1 cmd3'^M"                                     
screen -t tab2                                                                                     
select 1                                                                                           
stuff "echo 'tab2 cmd1'; echo 'tab2 cmd2'; echo 'tab2 cmd3'^M"                                     

altscreen on                                                                                       
term screen-256color                                                                               
bind ',' prev                                                                                      
bind '.' next                                                                                      
#                                                                                                  
#change the hardstatus settings to give an window list at the bottom of the                        
#screen, with the time and date and with the current window highlighted                            
hardstatus alwayslastline                                                                          
#hardstatus string '%{= kG}%-Lw%{= kW}%50> %n%f* %t%{= kG}%+Lw%< %{= kG}%-=%c:%s%{-}'              
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'

This technique makes use of screen's select and stuff commands to initially select one of the tabs, and then "stuff" a string into it.

screenshot

ss of screen w/ tabs & commands

3. Creating #2 without using a .screenrc file

If you're looking for the scenario where you can:

  1. create a screen session
  2. load it up with tabs
  3. have each tab running their own commands
  4. not require a .screenrc file

Then this is the one for you! Be prepared though. This one can get a little tricky with the command lines.

For starters let's create a screen session:

$ screen -AdmS myshell -t tab0 bash

The switches -AdmS do the following:

(See the screen man page for more details)

-A

    Adapt the sizes of all windows to the size of the  current terminal. 
    By default, screen tries to restore its old window sizes when
    attaching to resizable terminals

-d -m

    Start screen in "detached" mode. This creates a new session but
    doesn't attach to it. This is useful for system startup scripts.

-S sessionname

    When creating a new session, this option can be used to specify a
    meaningful name for the session. This name identifies the session for
    "screen -list" and "screen -r" actions. It substitutes the default
    [tty.host] suffix.

Now let's start loading it up with tabs + their commands:

$ screen -S myshell -X screen -t tab1 vim
$ screen -S myshell -X screen -t tab2 ping www.google.com
$ screen -S myshell -X screen -t tab3 bash

These 3 commands will create 3 additional tabs and run vim, ping google, and launch a bash shell. If we list out the screen sessions we'll see the following:

$ screen -ls
There is a screen on:
        26642.myshell   (Detached)
1 Socket in /var/run/screen/S-root.

If we connect to the screen session, myshell, and list the tabs that it contains we'll see the following:

$ screen -r myshell

Hit this key combination: Ctrl+A followed by Shift+"

Num Name                                                                   Flags

  0 tab0                                                                       $
  1 tab1                                                                       $
  2 tab2                                                                       $
  3 tab3                                                                       $

Switching to tab2:

64 bytes from ord08s08-in-f20.1e100.net (74.125.225.116): icmp_seq=443 ttl=55 time=41.4 ms
64 bytes from ord08s08-in-f20.1e100.net (74.125.225.116): icmp_seq=444 ttl=55 time=33.0 ms
64 bytes from ord08s08-in-f20.1e100.net (74.125.225.116): icmp_seq=445 ttl=55 time=30.1 ms

screenshot

ss of screen initiated from cli

The above commands are the basic way to accomplish what the OP was looking for. This of course can be condensed and refined using Bash aliases or even shell scripts, this is merely to demonstrate the capability and show the way!

References

  • 2
    If I do not miss-read the Q he wants to execute a command upon start of session. Typically screen "cd /foo/bar/; summon_cthulhu;" <<-- in that screen session – Runium May 5 '13 at 20:02
  • You think he means, open a screen with tabs, and run a command inside of one of them? – slm May 5 '13 at 20:08
  • Yes. Run some shell command at startup within screen, typically as when you log in after boot and get Last login on ... Welcome to XXX, N new mails..... At least that is how I read the heading of the Q. – Runium May 5 '13 at 20:48
  • Read his comments on the Q, he wants both. – slm May 5 '13 at 20:53
  • well done i would like to vote up too but my minor reputation prevented me :) – Ahmed Zain El Dein May 6 '13 at 10:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.