【linux】使用screen实现SSH远程连接断开后程序继续运行

1. 安装screen

  • ubuntu
sudo apt-get install screen
  • CentOS
yum install screen

2. screen常用命令

2.1 创建session

执行下方命令会创建一个名为yourSessionName的session,并进入了这个session

screen -S yourSessionName

2.2 退出session(session状态是detached,但session中的程序仍在运行)

  • 在当前服务器别的shell窗口输入:
screen -d yourSessionName # yourSessionName被detached ,但是session所在的shell窗口还在

在这里插入图片描述

screen -D yourSessionName # yourSessionName被detached ,并且session所在的shell窗口也logout

在这里插入图片描述

  • 或是直接把attach了当前session的shell窗口关闭

2.3 返回session

screen -r yourSessionName 

如果yourSessionName 已经在其他窗口

screen -dr yourSessionName #将yourSessionName从其他地方detach再reattach到当前位置 

2.4 查看当前所有session

screen -ls

在这里插入图片描述

session的代号.名字, session的创建时间, session的链接状态

session的代号和名字是等价的,使用任意一个或者两个合一起的全称都可以对该seesion做操作

screen -r 50930
screen -r pcgc
screen -r 50930.pcgc

2.5 删除session(session直接被删除,session中的程序也不再运行)

#先进入要退出的session
screen -r yourSessionName
#进入之后,再输入:
exit
# 然后shell上会输出[screen is terminating],并且这个session已经被删除了

3. screen 完整命令

(base) fanxing@amax:~$ screen -h
Use: screen [-opts] [cmd [args]]
 or: screen -r [host.tty]

Options:
-4            Resolve hostnames only to IPv4 addresses.
-6            Resolve hostnames only to IPv6 addresses.
-a            Force all capabilities into each windows termcap.
-A -[r|R]     Adapt all windows to the new display width & height.
-c file       Read configuration file instead of '.screenrc'.
-d (-r)       Detach the elsewhere running screen (and reattach here).
-dmS name     Start as daemon: Screen session in detached mode.
-D (-r)       Detach and logout remote (and reattach here).
-D -RR        Do whatever is needed to get a screen session.
-e xy         Change command characters.
-f            Flow control on, -fn = off, -fa = auto.
-h lines      Set the size of the scrollback history buffer.
-i            Interrupt output sooner when flow control is on.
-l            Login mode on (update /var/run/utmp), -ln = off.
-ls [match]   or
-list         Do nothing, just list our SockDir [on possible matches].
-L            Turn on output logging.
-m            ignore $STY variable, do create a new screen session.
-O            Choose optimal output rather than exact vt100 emulation.
-p window     Preselect the named window if it exists.
-q            Quiet startup. Exits with non-zero return code if unsuccessful.
-Q            Commands will send the response to the stdout of the querying process.
-r [session]  Reattach to a detached screen process.
-R            Reattach if possible, otherwise start a new session.
-s shell      Shell to execute rather than $SHELL.
-S sockname   Name this session <pid>.sockname instead of <pid>.<tty>.<host>.
-t title      Set title. (window's name).
-T term       Use term as $TERM for windows, rather than "screen".
-U            Tell screen to use UTF-8 encoding.
-v            Print "Screen version 4.03.01 (GNU) 28-Jun-15".
-wipe [match] Do nothing, just clean up SockDir [on possible matches].
-x            Attach to a not detached screen. (Multi display mode).
-X            Execute <cmd> as a screen command in the specified session.
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁星知微

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值