我有几个关于屏幕的问题:当我输入 时screen -r
,我得到以下信息:
There are several suitable screens on:
25154.tracks (Detached)
29278.mywork (Detached)
29138.mywork (Detached)
30915.mywork (Detached)
20065.mywork (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.
我不确定,但我相信这些屏幕大部分都很旧,因为我已经有一段时间没有使用屏幕了,而且我只是不小心从其中一个上拆了下来。对此,我的问题是:
- 有没有办法恢复最近分离屏幕?
- 如何“删除”较旧的屏幕会话?
- 是否可以显示每个会话中的日期和屏幕数量?
- 有没有办法暂时将别名与列出的屏幕关联起来
screen -r
以方便他们的选择?例如,如果screen -r
列出的屏幕如下所示,那就太好了:
There are several suitable screens on: [1] 25154.tracks (Detached) [2] 29278.mywork (Detached) [3] 29138.mywork (Detached) [4] 30915.mywork (Detached) [5] 20065.mywork (Detached) Choose one to resume:
然后我可以只输入 1、2、3、4 或 5,而不必输入pid
我想要恢复的整个屏幕。有什么办法可以解决这个问题吗?
答案1
这是一个应该适合您的脚本。
#!/bin/bash
function chooser {
echo
echo "I found the following screen sessions: "
echo
pcount=0
#
# find the session dir
#
sessdir=$( screen -ls | egrep 'Socket' | awk '{print $NF}' | sed -e 's/\.$//' )
#
# enumerate existing sessions, and add them to the plist() array.
#
for screen in $( find $sessdir -type p ); do
pcount=$((pcount+1))
pname=$( basename $screen )
pdate=$( ls -latr $screen | awk '{print "( "$6" "$7" "$8" )"}')
plist[$pcount]=${pname}
echo " [$pcount] $pname $pdate"
done
echo
echo -n "Please select a session to reconnect to: "
read choice
#
# if the selected choice doesn't exist, recycle the chooser.
#
if [ -z ${plist[$choice]} ]; then
echo
echo "Your choice [$choice] is invalid. Please try again."
echo
sleep 1
chooser
else
screen -r -d ${plist[$choice]}
fi
}
#
# the chooser function does all the work
#
chooser
我没有区分当前附加或分离的会话,因此如果这对您很重要,您可能必须自己执行此操作。
答案2
首先要做的是确定会话目录的位置。您可以从 的输出中得到这一点screen -ls
。
# session directory
sessdir=`screen -ls | sed -ne 's/.*Sockets* in \(.*\)\.$/\1/p'`
# display age of sessions:
ls -l $sessdir
# newest session
newest=`ls -1t $sessdir | head -1`
# Kill all sessions but newest
ls -1t $sessdir| sed 1d | while read sess; do screen -m -S $sess -X quit; done
“最新”会话是最近创建的会话;我不相信有任何关于会话分离时的信息。
您当然可以创建一个包装器来从列表中进行选择并启动该会话,但是您无法从现有会话中检索信息(输出将转到会话,而不是调用程序的输出)。