Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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:

  1. run screen
  2. enter command
  3. CTRL+A
  4. CTRL+C
  5. enter command
  6. CTRL+A
  7. CTRL+D

How can I do this automatically in a script? A simple example would help me a lot.

Thank you for replies.

share|improve this question
    
I don't understand what you mean by a window in the same screen session. Do you want to have two windows open viewing the same process's output? What are you using this for? –  wingedsubmariner Oct 30 '13 at 14:31

2 Answers 2

up vote 0 down vote accepted

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 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.

share|improve this answer
    
Thanks, I need run two scripts that will run permanently. It will be independent of each other... And both must run in the background... And I need run these scripts after start of Debian. I hope that now it is clear. –  Mato Oct 30 '13 at 16:22
    
@Mato, please try to understand the script - all you need to do is remove the "touch" command and the while loop that waits for the file to appears. You can change the "zombie xy" to "zombie xy onerror" so that the windows will only stay around if the commands terminate with an error or a signal, and you can skip the entire screenrc stuff if you don't ever care about the commands after they exit or are killed. –  Gabe Oct 30 '13 at 16:28
    
Done,thank you very much. –  Mato Oct 30 '13 at 16:50
    
and if you can ask you too... How Can I it correct close with inid.d script? Or how I can it correct terminate or restart with init.d script? –  Mato Oct 30 '13 at 16:52
    
You need to send the "quit" command to the screen session. Save the $SESSION variable somewhere, and terminate the whole thing with "screen -S $SESSION -X quit". Look at "man screen" for more information. –  Gabe Oct 30 '13 at 17:01

screen -dm creates a session in background running the command you specified.

So if you put

screen -dm vi xxx
screen -dm vi yyy

in 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 yyy

you can choose the session you want to attach to with screen -r session1 or screen -r session2

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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