I'm not exactly sure what you want to happen - do you want a script that creates the screen session with two windows for two commands, or do you want to run a script in a screen window that runs one command, then creates a new window for the second one?
The second one's easy, so let's start with that:
#!/bin/bash
command1
screen command2
Running "screen " within screen will create a new window in the current session, not start a new one. But it will return immediately, so after the last line there, the script will exit while command2 is still running. And when command2 is done, its window will close.
The first interpretation of your question a bit harder anyway, so let's go ahead and solve the above while at it:
#!/bin/bash
# Need to positively identify the session name:
SESSION=mysession.$$
echo "TO ATTACH TO SESSION: screen -r ${SESSION}"
# For signalling and stuff
FLAGDIR=$(mktemp -d)
# To keep the windows around after the commands are done,
# set the "zombie" option (see the man-page)
echo "source $HOME/.screenrc" > ${FLAGDIR}/screenrc
echo "zombie xy" >> ${FLAGDIR}/screenrc
# Use that temporary screenrc; create a detached session:
screen \
-c ${FLAGDIR}/screenrc \
-d -m \
-S ${SESSION} \
bash -c "command1 ; touch ${FLAGDIR}/done"
# Wait for command1 to terminate
while [[ ! -f ${FLAGDIR}/done ]] ; do sleep 1 ; done
# Now start command2 in a new window, by sending a remote command:
screen -S $SESSION -X screen command2
# Don't need this any more:
rm -rf ${FLAGDIR}
The script will launch command1
, wait until it's done, then launch command2
and exit. As if you'd run command1 ; command2 &
, but with the output elsewhere. I'm sure you can figure out how to run command1
in the background.