how do you script gnu screen from within a screen session to open new windows and run commands in them?

让人想犯罪 __ 提交于 2020-03-21 05:17:09

问题


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:


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

#!/bin/bash

screen vi
screen top



回答2:


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.

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



来源:https://stackoverflow.com/questions/2156290/how-do-you-script-gnu-screen-from-within-a-screen-session-to-open-new-windows-an

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!