当前位置: 首页 > 工具软件 > Split Screen > 使用案例 >

ubuntu中screen的使用

郑宇
2023-12-01

screen概念:

“screen命令类似于“虚拟桌面”的概念,可以启动一个Linux下的虚拟桌面,在其中可以和普通终端一样执行各种命令。”

screen可以实现当前窗口和任务的分离,即使在离线状态下,我们在服务器上的程序仍能在后台运行。重新登录到服务器后,读取窗口进程,就能连接到对应的程序窗口。

一、需求背景:

1)ubuntu环境中需要同时启动前端和后端,但是两者不能同时在一个窗口中运行;

2)如果ubuntu环境过一段时间不使用,会被自动退出,此时启动的进程也被终止。

二、业务实现

1.检查ubuntu中是否有screen,没有则安装

sudo apt-get install screen

2.安装完成之后,我们还是进入到上次我们创建的文件夹 

screen -ls     #显示当前用户所拥有的scree详情
sudo su
screen -S myserver    #1.创建一个名为 myserver的screen
conda activate web
cd MyService/myserver/
python myserver.py    #在窗口myserver中运行后端python程序
Ctrl+a d    #退出当前screen

screen -S myclient #2.创建一个名为myclient 的screen
conda activate web
cd  MyService/myclient/
npm run serve    #在窗口lin中运行前端程序
Ctrl+a d

screen -ls   #显示当前用户所拥有的scree详情
screen -r myclient     #进入窗口myclient     
screen -r myserver    #进入窗口myserver   
Ctrl+a d  #退出当前窗口

这样,开两个screen窗口分别运行前端和后端,即使关掉xshell之后网站也会在后台继续跑 不会被关闭啦。

三、screen常用命令总结

               Ctrl+a ?    显示所有键绑定信息
               Ctrl+a w    显示所有窗口列表
               Ctrl+a C-a    切换到之前显示的窗口
               Ctrl+a c    创建一个新的运行shell的窗口并切换到该窗口
               Ctrl+a n    切换到下一个窗口
               Ctrl+a p    切换到前一个窗口(与C-a n相对)
               Ctrl+a 0..9    切换到窗口0..9
               Ctrl+a a    发送 C-a到当前窗口
               Ctrl+a d    暂时断开screen会话
               Ctrl+a k    杀掉当前窗口
               Ctrl+a [    进入拷贝/回滚模式

1)List all screen sessions of current user
#screen -ls

2)To see all screen sessions on a specific machine
#ls -laR /var/run/screen/

3)To see all commands or parameters on screen.
Ctrl + a, Then Press ?

4)Open a new screen session
#screen

5)Detach/Exit from a screen session
Ctrl + a, Then Press d

6)Kill a screen session
Ctrl + a, Then Press Shift + k

7)Reattach to a screen session
#screen -r session_id_or_name

8)How to create multiple screen window/tab in a screen session
Ctrl + a, Then Press c

9)How to move to next screen window/tab in a screen session
Ctrl + a, Then Press n

10)How to move to previous screen window/tab in a screen session
Ctrl + a, Then Press p

11)How to goto n'th screen window/tab
Ctrl + a, Then Press '

12)How to list name of all screen windows/tabs and select from there
Ctrl + a, Then Press Shift + '

13)How to switch to screen window/tab 0 - 9
Ctrl + a, Then Press 0 -9

14)
How to Toggle  to  the  window/tab  displayed  previously
Ctrl + a, Then Ctrl + a again

15)How to change the name of screen window/tab
Ctrl + a, Then Press Shift + a

16)How to clear a screen window/tab
Ctrl + a, Then Press Shift + c

17)How to Kill all windows and terminate screen
Ctrl + a, Then Press \
screen -ls

18)How to lock a screen session
Ctrl + a, Then Press x

19)How to log a screen session
You will find screenlog.0 file in your home directory.
Ctrl + a, Then Press Shift + h
or
#screen -L (Capital L)

20)reattach to a specific window/tab in a screen session
#screen -r session_id_or_name -p tab_num_or_name

21)If a session is running, then reattach. If necessary detach and logout remotely first. If it was not running create it and notify the user.
#screen -D -R

22)How to create new screen session and specify a meaningful name for the session.
#screen -S session_name

23)Howto Attach to a not detached screen session.
#screen -x

24)How to split screen
a)
Split the Window
Horizontally
Ctrl + a, Then Press Shift + s
or
Vertically
Ctrl + a, Then Press Shift + \

b)
Switch between spilted windows
Ctrl + a, Then Press Tab
or
Ctrl + a, Then Type :focus
* Here :focus is a command

c)
In the spited window use following command to open existing session
Ctrl + a, Then Press 0-9
or
Ctrl + a, Then Press n or p
or
Ctrl + a, Then Press Shift + '
or
Ctrl + a, Then Presss c

d)
Resize a splitted window/region
Ctrl + a, Then Type :resize 25
* Here :resize is a command

e)
Remove current splitted window/region
Ctrl + a, Then Type :remove
* Here :remove is a command
or
Ctrl + a, Then Press Shift + x

f)
Remove all spiltted windows/regions except the current one.
Ctrl + a, Then Type :only
* Here :only is a command
or
Ctrl + a, Then Press Shift +q

g)
Change color of split bar (border)
http://michael-prokop.at/computer/config/.screenrc
#vim ~/.screenrc
sorendition 10 74

h)
Close the screen and all regions
Ctrl + a, Then Press \

25)How to rename an existing session
screen -ls
screen -x old_session_name
Ctrl + a, Then Type :sessionname new_session_name
*Here :sessionname is a command

 类似资料: