views:

365

answers:

2

From within a screen session, I'd like to run a shell script that opens a few new screen windows in the same session and start running some programs in them.

I need a script like this:

screen -t newWindow
[switch to newWindow and execute a command]
screen -t newWindow2
[switch to newWindow2 and execute a command]

I don't know how to accomplish the effect I describe in the brackets. Any clues? Please note that this is not a script I'll be running to start a screen session. I need this script to be runnable within an existing screen session, in order to add new windows to the session.

+1  A: 

Running this script inside screen does what I think you want:

#!/bin/bash

screen vi
screen top
P-Nuts
That doesn't seem to work if you run it within an existing screen session.
dan
Hmm. Seems to be working for me using "Screen version 4.00.03jw4 (FAU) 2-May-06" on Ubuntu Karmic. Got rid of my `.screenrc` to check nothing in there was relevant. Ran `screen` to make a new session. `C-a"` shows one window `bash`. Ran `./aa.sh` which is is the above script. Now I see `vi`, and `C-a"` lists `bash`, `vi`, and `top`.
P-Nuts
More generally, what's important is the value of the environmental variable `STY`. If I run `screen -list` to find the name of my session, and then do `STY=32720.pts-1.lundy ./aa.sh` from outside the session, then the commands get launched in the existing session. It works inside the session because `STY` gets set automatically; confirm this with `echo $STY`.
P-Nuts
I finally did manage to get it to work! Thanks.
dan
dan
P-Nuts
+1  A: 

Note: you can't launch script working following way from a screen session. And it will open in session no tabs... Its more a related tip than a real answer to the question.

There is an other solution, if you accept to have a screen session by running process...

new session script

#!/bin/sh
echo "nouvelle session screen ${sessionName}"
screen -S ${sessionName}  init.sh
echo "screen session: done"
echo "go to ${AnyWhere}"
sleep 1
screenexec ${sessionName} "cd ${AnyWhere}"

init script (here "init.sh")

#!/bin/zsh
zsh -c "sleep 0.2"
screen -d #detach the initialised screen
zsh       #let a prompt running

injection script (here screenexec)

#!/bin/sh
# $1 -> nom de screen cible  $2 -> commande
echo "injection de «${2}» dans la session «${1}» ..."
screen -x "$1" -X stuff "$2"              #inject the command
screen -x "$1" -X eval "stuff \015"       #inject \n
echo "Done"

By using this way, you should inject code easily in your screens, interesting if your script act like a deamon...

For those who prefer script in python, I've made a small lib to create sessions, close sessions, inject commands: ScreenUtils.py

It's a small project, which don't handle multiwindows screen sessions.

christophe31