17

I'm using screen after I have logged in with ssh to my server. As of now I set up the splits in my screen window by hand and run the commands by hand as shown in the following screen-shot:

enter image description here

  • The top part should run tail -n 1 -f /home/server/log/access.log.
  • The lower right part should run htop
  • The lower left one should simply be a command prompt

Is there any way to have that be done via commands/script, so I not have to redo it every time by hand?

Videonauth
  • 33,355
  • 17
  • 105
  • 120

2 Answers2

19

For the specific case of window arrangements, there's a screen command to save them to a file: layout dump. From man screen:

layout dump [filename]

Write to a file the order of splits made in the current layout. This is
useful to recreate the order of  your  regions  used  in  your  current
layout.  Only  the  current  layout is recorded. While the order of the
regions are recorded, the sizes of  those  regions  and  which  windows
correspond  to  which regions are not. If no filename is specified, the
default is layout-dump, saved in the directory that the screen  process
was  started in. If the file already exists, layout dump will append to
that file. As an example:

           C-a : layout dump /home/user/.screenrc

will save or append the layout to the user's .screenrc file.

So, once you make the arrangement manually, press Ctrla:, then type layout dump /path/to/some/file. The layout will be saved to /path/to/some/file and you can then restore it in a new session with:

screen -c /path/to/some/file
muru
  • 197,895
  • 55
  • 485
  • 740
  • +1 nice one; The split -v seems undocumented :) this is why I was struggling. – Videonauth Nov 28 '17 at 01:55
  • @Videonauth it actually is documented under default keybindings section. C-a | (split -v) Split the current region vertically into two new ones. – Sergiy Kolodyazhnyy Nov 28 '17 at 02:01
  • Yes, it looks it's only mentioned in the manpage, next to C-a |, but not in the GNU docs – muru Nov 28 '17 at 02:01
  • 1
    Found an odd behavior you might want to mention: If you type for example layout dump ~/layout the process will fail, it need to full path (i.e. /home/$USER/layout) – Videonauth Nov 28 '17 at 09:37
  • @Videonauth since tilde expansion is usually done by the shell, it's not surprising if a given command doesn't support it internally. Some do, most don't. – muru Nov 28 '17 at 09:39
  • I can't dump the layout from a detached session by following: screen -r SESSION_NAME -X layout dump FILE_NAME. Other commands work fine with -X. – dojuba Sep 19 '18 at 08:47
15

I came up with the following to create the output shown in my question and following @muru's excellent answer. Using layout dump gave me the following:

split
focus
split -v
focus

Note: Tilde (~) expansion does not work with layout dump so instead of ~/layout.dmp for example you would need to use /home/<username>/layout.dmp.

From which I then created the following .screenrc

# create the top screen
chdir /home/server/log
screen -t "Apache Log" tail -n 1 -f access.log
# split the screen and focus onto the new created space
split
focus
#create the bash
chdir /home/server/log
screen
# split vertically and focus onto the new area
split -v
focus
# create the htop screen
screen -t "Htop" htop
# focus twice to end up with the bash area active
focus
focus

Now I only need to type screen and get my wanted layout started. I leave that here as an example for those who are wondering, but don't forget to up-vote @muru's answer, since he is the one who made me able to solve this.

Videonauth
  • 33,355
  • 17
  • 105
  • 120
  • Thanks to your answer, I was able to come up with a setup I wanted. I put it in a more specifically named file (e.g. example.screenrc) and am launching screen using screen -c example.screenrc. Like you mentioned, ~ doesn't work in the rc file, however $HOME does. Another thing I noted is that the screen command only supports simple (literal) arguments. Naturally, it's not a shell with all the fancy expansions etc. If these are needed, can just use, as an example, screen -t "http-server" bash -c 'http-server --hostname $(hostname -s)'. – Gene Pavlovsky Nov 15 '22 at 15:19