screen命令#伪服务

大家有没有碰上这么一种苦恼,通过ssh去连接某服务器,然后运行了一个长时间的任务不敢断开;或者启动了一个软件,希望它一直运行,可是又不想一直保持ssh连接。

当然了,再此之前你应该也尝试了各种五花八门的骚操作,包括“&”或者nohup等命令,但实际使用这些命令并不稳定,所以在这里也再次整理下screen命令的用法,案例一下。

#安装screen
yum install -y screen

老规矩,我们先来简单看下最新的功能列表:

#options
-4            Resolve hostnames only to IPv4 addresses.
-6            Resolve hostnames only to IPv6 addresses.
-a            Force all capabilities into each window's 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.01.00devel (GNU) 2-May-06". -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. 

接下来进行一下最基础也是最实用的流程操作
screen
打开一个新的screen会话,你可能没觉得有什么变化,但你的当前会话已经属于一个新的screen了。

 
screen


接下来我们运行一个frp,frp就是一个程序,而非服务,所以我们在每次使用的时候都要去手动开启它。

 
frps


Ctrl+A+D
Ctrl+A+D连按挂起会话,返回到进入会话前的位置

 
挂起会话


screen -ls
screen -ls用户查看当前存在的会话详情

 
查看会话


screen -r ID
screen -r 进入会话,当然-r参数后需要加子界面id号,即上图看到的3061.

 
进入会话


当然,你如果开的会话太多,怕混乱,可以使用-S参数进行会话命名,一般来讲,简单的这几个命令已经足够日常所需了。
现在我们可以随意断开ssh连接了,但我们的frp小程序却像个服务一样稳定的在服务器上运行着,需要类似功能的程序还有很多,如鸟群的qqbot机器人,再如一些python写的页面小程序,都可以借此稳定常驻进程。

猜你喜欢

转载自www.cnblogs.com/rabbitmask/p/10200407.html