对懒人而言,screen 很简单(三板斧) by oflyhigh

View this thread on steempeak.com
· @oflyhigh ·
$74.19
对懒人而言,screen 很简单(三板斧)
我们在终端运行程序时,退出终端或者终端异常断掉,这时候由于SIGHUP信号会导致我们的程序退出。

<center>![](https://steemitimages.com/DQmVeiMb8Qrqm2GozGF1Wnq6DmGt65LDXYvQavHET3mGxnZ/image.png)
(图源 :https://en.wikipedia.org/wiki/GNU_Screen)</center>

为了让终端退出(或断掉)后,程序能正常运行,一般情况我都使用**`nohup`**来运行程序,但是**`nohup`**有个问题,它将输出内容输出到nohup文件中,而这个文件还不是实时更新的,这样当一些问题发生时,我想看到实时的输出信息,那是看不到的。

当然,这个问题可以通过在程序中启用日志等方式解决,但是终归是多了一些麻烦,懒人表示拒绝费劲的方案。

那这时候,就该**`screen`**闪亮登场了!没错,**`screen`**就是懒人用于解决上述问题的极佳方案。

# 简介 & 安装

以下是我对**`screen`** 的理解。

**`screen`** 可以为在你的终端(A)上你虚拟出一个终端(V),你在这个终端(V)上进行操作、运行程序,终端(A)退出或断掉后,终端(V)不受影响,程序会继续运行。你可以通过重新登陆终端(A)来回到终端(V)上。

好吧,有点拗口,也未必准确,不过大致就是这样啦。

安装**`screen`** 很简单,我的系统(Ubuntu)下,执行下列指令即可:
`sudo apt-get update`
`sudo apt-get upgrade`
`sudo apt-get install screen`

# 简单用法

可以通过如下命令查询帮助信息
`screen --help`

或者查看手册
`man screen`

但是其实没必要记那么多啦,我认为几个足矣

#### 创建虚拟终端

直接键入
`screen`

或者使用
`screen -S xxx`
为虚拟终端起个名字

这个终端看起来和原来shell一般无二哦,这时候我们就可以在里边执行程序喽。

#### 列出虚拟终端

假设我们的终端链接突然关闭或者断掉,我们重新爬上来,那么怎么找回我们原来的窗口呢?

答案是使用如下指令:
`screen -ls`

![](https://steemitimages.com/DQmajTc4zAPEqEmdn8Dncn9LXpyKGBvza1gjhtgXnVzeG6R/image.png)
这就是我刚刚创建的虚拟终端

#### 恢复连接

好了,我们找到了我们之前创建的终端,那么如何连上去呢?

答案也是超级简单
对于上述虚拟终端而言,执行如下指令即可:
`screen -r 1862.oflyhigh`

一下子就连回来喽,还停止在之前的界面。

# 其它

有了上述三把板斧,对于我来讲已经足够用了。当然,有时候可能需要多个虚拟终端,但是用法和之前的介绍一般无二。

唯一遇到过一点问题的是,虚拟终端在那,死活连不上,仔细查了一下,虚拟终端显示**`Attached`**,但实际上我那个窗口已经崩溃掉了,screen没及时更新状态,导致我连不上。

这时候我们只需执行类似如下指令即可:
`screen -D  -r 1862.oflyhigh`

指令解释如下:
>-D (-r)       Detach and logout remote (and reattach here).

想了解更多或者用出更多花样的,看手册吧。

**懒人的宗旨,够用即可!**
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 79 others
properties (23)
post_id41,125,343
authoroflyhigh
permlinkscreen
categoryscreen
json_metadata"{"app": "steemit/0.1", "format": "markdown", "links": ["https://en.wikipedia.org/wiki/GNU_Screen"], "image": ["https://steemitimages.com/DQmVeiMb8Qrqm2GozGF1Wnq6DmGt65LDXYvQavHET3mGxnZ/image.png"], "tags": ["screen", "linux", "tools", "cn", "cn-programming"]}"
created2018-03-29 12:58:42
last_update2018-03-29 12:58:42
depth0
children16
net_rshares24,533,989,214,970
last_payout2018-04-05 12:58:42
cashout_time1969-12-31 23:59:59
total_payout_value62.749 SBD
curator_payout_value11.443 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length1,533
author_reputation1,148,153,621,496,884
root_title"对懒人而言,screen 很简单(三板斧)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (143)
@cifer · (edited)
$0.37
O 哥,俺觉得 tmux 更好用 :D
👍  , ,
properties (23)
post_id41,128,818
authorcifer
permlinkre-oflyhigh-screen-20180329t132533988z
categoryscreen
json_metadata"{"app": "steemit/0.1", "tags": ["screen"]}"
created2018-03-29 13:25:36
last_update2018-03-29 13:26:06
depth1
children2
net_rshares123,950,428,529
last_payout2018-04-05 13:25:36
cashout_time1969-12-31 23:59:59
total_payout_value0.280 SBD
curator_payout_value0.091 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length19
author_reputation7,963,630,681,040
root_title"对懒人而言,screen 很简单(三板斧)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (3)
@oflyhigh ·
感谢分享
properties (22)
post_id41,196,098
authoroflyhigh
permlinkre-cifer-re-oflyhigh-screen-20180329t224147466z
categoryscreen
json_metadata"{"app": "steemit/0.1", "tags": ["screen"]}"
created2018-03-29 22:41:48
last_update2018-03-29 22:41:48
depth2
children1
net_rshares0
last_payout2018-04-05 22:41:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length4
author_reputation1,148,153,621,496,884
root_title"对懒人而言,screen 很简单(三板斧)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@netscape101 ·
tmux+tmux ressurect plugin
https://github.com/tmux-plugins/tmux-resurrect
properties (22)
post_id41,258,215
authornetscape101
permlinkre-oflyhigh-re-cifer-re-oflyhigh-screen-20180330t091841739z
categoryscreen
json_metadata"{"app": "steemit/0.1", "links": ["https://github.com/tmux-plugins/tmux-resurrect"], "tags": ["screen"]}"
created2018-03-30 09:18:45
last_update2018-03-30 09:18:45
depth3
children0
net_rshares0
last_payout2018-04-06 09:18:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length73
author_reputation45,825,911,400
root_title"对懒人而言,screen 很简单(三板斧)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@apkdl ·
cn blog very nice follow you and vote thanks..
properties (22)
post_id41,131,356
authorapkdl
permlinkre-oflyhigh-screen-20180329t134455348z
categoryscreen
json_metadata"{"app": "steemit/0.1", "tags": ["screen"]}"
created2018-03-29 13:44:57
last_update2018-03-29 13:44:57
depth1
children0
net_rshares0
last_payout2018-04-05 13:44:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length46
author_reputation982,250,417,825
root_title"对懒人而言,screen 很简单(三板斧)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@jasper123chew ·
screen is indeed powerful, thanks
properties (22)
post_id41,134,858
authorjasper123chew
permlinkre-oflyhigh-screen-20180329t140953124z
categoryscreen
json_metadata"{"app": "steemit/0.1", "tags": ["screen"]}"
created2018-03-29 14:09:57
last_update2018-03-29 14:09:57
depth1
children0
net_rshares0
last_payout2018-04-05 14:09:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length33
author_reputation2,423,507,926
root_title"对懒人而言,screen 很简单(三板斧)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@maiyude ·
没看到做什么的,但是感觉很厉害。
properties (22)
post_id41,139,909
authormaiyude
permlinkre-oflyhigh-screen-20180329t144440461z
categoryscreen
json_metadata"{"app": "busy/2.4.0", "community": "busy", "tags": ["screen"]}"
created2018-03-29 14:44:54
last_update2018-03-29 14:44:54
depth1
children0
net_rshares0
last_payout2018-04-05 14:44:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length16
author_reputation21,161,943,744,615
root_title"对懒人而言,screen 很简单(三板斧)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@huawuya ·
$0.37
我现在后台运行基本都用screen,ctrl a+d也是要记住的,另外我用
> screen -r 1862

就可以恢复连接。
用了screen,再也不愁退出终端导致的后台程序退出了。
👍  
properties (23)
post_id41,151,486
authorhuawuya
permlinkre-oflyhigh-screen-20180329t160742796z
categoryscreen
json_metadata"{"app": "busy/2.4.0", "community": "busy", "tags": ["screen"]}"
created2018-03-29 16:07:45
last_update2018-03-29 16:07:45
depth1
children4
net_rshares122,243,064,028
last_payout2018-04-05 16:07:45
cashout_time1969-12-31 23:59:59
total_payout_value0.275 SBD
curator_payout_value0.091 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length93
author_reputation16,426,897,814
root_title"对懒人而言,screen 很简单(三板斧)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@oflyhigh ·
我都直接关窗口
Ctrl a+d 只是偶尔用😊
properties (22)
post_id41,227,468
authoroflyhigh
permlinkre-huawuya-re-oflyhigh-screen-20180330t040915644z
categoryscreen
json_metadata"{"app": "steemit/0.1", "tags": ["screen"]}"
created2018-03-30 04:09:18
last_update2018-03-30 04:09:18
depth2
children3
net_rshares0
last_payout2018-04-06 04:09:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length23
author_reputation1,148,153,621,496,884
root_title"对懒人而言,screen 很简单(三板斧)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@huawuya ·
我倒是经常用,因为还要继续干其他事 😁
properties (22)
post_id41,272,292
authorhuawuya
permlinkre-oflyhigh-re-huawuya-re-oflyhigh-screen-20180330t113623756z
categoryscreen
json_metadata"{"app": "busy/2.4.0", "community": "busy", "tags": ["screen"]}"
created2018-03-30 11:36:45
last_update2018-03-30 11:36:45
depth3
children2
net_rshares0
last_payout2018-04-06 11:36:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length19
author_reputation16,426,897,814
root_title"对懒人而言,screen 很简单(三板斧)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@qifenggan ·
$0.03
挖矿必备技能 :)
👍  ,
properties (23)
post_id41,159,746
authorqifenggan
permlinkre-oflyhigh-screen-20180329t171125394z
categoryscreen
json_metadata"{"app": "steemit/0.1", "tags": ["screen"]}"
created2018-03-29 17:11:27
last_update2018-03-29 17:11:27
depth1
children0
net_rshares11,088,991,276
last_payout2018-04-05 17:11:27
cashout_time1969-12-31 23:59:59
total_payout_value0.026 SBD
curator_payout_value0.005 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length9
author_reputation12,053,442,795,680
root_title"对懒人而言,screen 很简单(三板斧)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (2)
@steemitboard ·
Congratulations @oflyhigh! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

[![](https://steemitimages.com/70x80/http://steemitboard.com/notifications/firstpayout.png)](http://steemitboard.com/@oflyhigh) You got your First payout
[![](https://steemitimages.com/70x80/http://steemitboard.com/notifications/payout.png)](http://steemitboard.com/@oflyhigh) Award for the total payout received

Click on any badge to view your own Board of Honor on SteemitBoard.

To support your work, I also upvoted your post!
For more information about SteemitBoard, click [here](https://steemit.com/@steemitboard)

If you no longer want to receive notifications, reply to this comment with the word `STOP`

> Upvote this notification to help all Steemit users. Learn why [here](https://steemit.com/steemitboard/@steemitboard/http-i-cubeupload-com-7ciqeo-png)!
properties (22)
post_id41,179,139
authorsteemitboard
permlinksteemitboard-notify-oflyhigh-20180329t195053000z
categoryscreen
json_metadata"{"image": ["https://steemitboard.com/img/notifications.png"]}"
created2018-03-29 19:50:51
last_update2018-03-29 19:50:51
depth1
children0
net_rshares0
last_payout2018-04-05 19:50:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length883
author_reputation38,705,954,145,809
root_title"对懒人而言,screen 很简单(三板斧)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@justyy ·
$0.14
还可以:
1. 命令后面加个 & 
2. 先前端运行 命令,然后 ctrl+z, 然后 bg 放到后台。(jobs 列出后台的程序,fg 把后台的程序调到前台)
3. 已赞。
👍  
properties (23)
post_id41,223,221
authorjustyy
permlinkre-oflyhigh-screen-20180330t032838443z
categoryscreen
json_metadata"{"app": "steemit/0.1", "tags": ["screen"]}"
created2018-03-30 03:28:42
last_update2018-03-30 03:28:42
depth1
children2
net_rshares45,841,149,010
last_payout2018-04-06 03:28:42
cashout_time1969-12-31 23:59:59
total_payout_value0.101 SBD
curator_payout_value0.034 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length87
author_reputation2,057,469,156,047,835
root_title"对懒人而言,screen 很简单(三板斧)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@oflyhigh ·
后台运行避免不了SIGHUP信号,所以1,2的回复都是错的
3,已回赞
properties (22)
post_id41,227,661
authoroflyhigh
permlinkre-justyy-re-oflyhigh-screen-20180330t041127422z
categoryscreen
json_metadata"{"app": "steemit/0.1", "tags": ["screen"]}"
created2018-03-30 04:11:30
last_update2018-03-30 04:11:30
depth2
children1
net_rshares0
last_payout2018-04-06 04:11:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length35
author_reputation1,148,153,621,496,884
root_title"对懒人而言,screen 很简单(三板斧)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@justyy ·
奇怪了,我一直就是认为 & 和 bg 是可以的,今天查了一下:
- https://stackoverflow.com/questions/625409/how-do-i-put-an-already-running-process-under-nohup
- https://unix.stackexchange.com/questions/4004/how-can-i-close-a-terminal-without-killing-the-command-running-in-it
说了其实是可以通过 disown 这个命令实现同样的 nohup...
但是很奇怪是我并没有运行第三步,实际上一直都没有问题。难道我开了两个窗口,只要有一个开着就没事?
properties (22)
post_id41,263,025
authorjustyy
permlinkre-oflyhigh-re-justyy-re-oflyhigh-screen-20180330t100454911z
categoryscreen
json_metadata"{"app": "steemit/0.1", "links": ["https://stackoverflow.com/questions/625409/how-do-i-put-an-already-running-process-under-nohup", "https://unix.stackexchange.com/questions/4004/how-can-i-close-a-terminal-without-killing-the-command-running-in-it"], "tags": ["screen"]}"
created2018-03-30 10:04:57
last_update2018-03-30 10:04:57
depth3
children0
net_rshares0
last_payout2018-04-06 10:04:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length330
author_reputation2,057,469,156,047,835
root_title"对懒人而言,screen 很简单(三板斧)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000