English 中文(简体)
how do you script gnu screen from within a screen session to open new windows and run commands in them?
原标题:

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.

最佳回答

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

#!/bin/bash

screen vi
screen top
问题回答

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 15"       #inject 

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.

Forgot to mention I made a real python library out of it long ago: https://github.com/Christophe31/screenutils





相关问题
switching between screens

I m using GNU screen just recently. I m wondering if there is an easier way to switch between screen. for example to map it to alt+left arrow and alt+right arrow ?

Custom script in .screenrc

I made a script that spawns a remote shell or runs a local shell whether it s on the current machine or not: #!/bin/bash # By: benoror <benoror@gmail.com> # # spawns a remote shell or runs a ...

GNU Screen: Changing Global Key-binding

I ve recently started using screen in remote sessions. One problem is that emacs doesn t recognize its C-a and I don t want to bind emacs C-a to something else, as I m very used to it. Google shows ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签