How can I start new window in the same screen session automatically?
I read How can I start multiple screen sessions automatically?, but I don't understand the first accepted reply:
screen -dmS "$SESSION_NAME" "$COMMAND" "$ARGUMENTS"In my case I need to automatically create one screen session for one script, and afterwards I need to create a new window in the same session for another script. Manually, I would:
- run screen
- enter command
- CTRL+A
- CTRL+C
- enter command
- CTRL+A
- CTRL+D
How can I do this automatically in a script? A simple example would help me a lot.
Thank you for replies.
13 Answers
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 command2Running "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 command2and 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.
- Start session
startupand run in detached windowcommand1 Add new detached window and run
command2screen -dmS startup bash -c 'command1; exec bash' screen -S startup -x -X screen bash -c 'command2; exec bash'
Reattach the sessionstartup at any time
screen -r startup 1 screen -dm creates a session in background running the command you specified.
So if you put
screen -dm vi xxx
screen -dm vi yyyin your script, you will have 2 sessions, one editing xxx and editing yyy.
screen -ls will list the sessions, which you can attach to using screen -r <session number>
This is not very convenient as need to find the session number. That's where -S comes in.
screen -dmS session1 vi xxx
will create a detached session called session1 so if you change your script to
screen -dmS session1 vi xxx
screen -dmS session2 vi yyyyou can choose the session you want to attach to with screen -r session1 or screen -r session2