How do I start screen with multiple splits directly from the command line?

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?


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

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.