Linux nohup、&、screen命令(保证程序能够在断开ssh连接的情况下能够继续运行)


这里是一段防爬虫文本,请读者忽略。
本文原创首发于CSDN,作者IDYS
博客首页:https://blog.csdn.net/weixin_41633902/
本文链接:https://blog.csdn.net/weixin_41633902/article/details/108830808
未经授权,禁止转载!恶意转载,后果自负!尊重原创,远离剽窃!


写在开头的话

  • 请记住:实践是掌握知识的最快方法
  • 如果你只是怀着看看的态度去快速浏览文章,而不去认认真真的把文章里面讲的任何一个知识点去实践一遍,那么你永远也掌握不了它
  • 生命不息,折腾不止!

nohup、&、screen命令(保证程序能够在断开ssh连接的情况下能够继续运行)

00. nohup 命令

0.1 nohup 介绍

  • nohup 英文全称 no hang up(不挂起),用于在系统后台不挂断地运行命令,退出终端不会影响程序的运行。
  • nohup命令,在默认情况下(非重定向时),会输出一个名叫 nohup.out 的文件到当前目录下,如果当前目录的 nohup.out 文件不可写,输出重定向到 $HOME/nohup.out 文件中。

0.2 不使用 nohup 和 & 的程序(对其发送CTRL+C)

  • 编写一个循环脚本
[dayuanshuai@www nohuo_test] vim echo.sh     
  1 #! /bin/bash
  2 i=1
  3 while true
  4 do
  5 echo $i
  6 let i++
  7 sleep 3
  8 done

[dayuanshuai@www nohuo_test] chmod u+x echo.sh   # 对其添加一个 执行权限
  • 运行这个程序
[dayuanshuai@www nohuo_test] ./echo.sh 
1
2
3
4
5
6
7
8
  • 在另外一个终端查看该程序的运行状态,可以看到该程序在运行
[root@www ~] ps aux | grep echo.sh 
500        2067  0.0  0.0 106076  1356 pts/0    S+   17:54   0:00 /bin/bash ./echo.sh
root       2080  0.0  0.0 103256   880 pts/2    S+   17:54   0:00 grep echo.sh
  • 这个时候我们在运行的程序执行是,ctrl+c发送中断信号。
[dayuanshuai@www nohuo_test] ./echo.sh 
1
2
3
4
5
6
7
8
9
10
11
12
13
^C
  • 这个时候再次查看运行状态发现程序已经不再运行
[root@www ~] ps aux | grep echo.sh
root       2086  0.0  0.0 103256   876 pts/2    S+   17:55   0:00 grep echo.sh

0.3 不使用 nohup 和 & 的程序(断开与终端的连接)

  • A终端里面执行死循环程序
[dayuanshuai@www nohuo_test] ./echo.sh 
1
2
3
4
  • B终端查看其运行状态,发现程序正在运行
[root@www ~] ps aux | grep echo.sh
500        2126  0.0  0.0 106076  1356 pts/0    S+   18:04   0:00 /bin/bash ./echo.sh
root       2132  0.0  0.0 103256   880 pts/1    R+   18:04   0:00 grep echo.sh
  • 断开与A终端的连接,再次在B终端查看程序运行状态,发现程序已经不再运行
[root@www ~] ps aux | grep echo.sh
root       2136  0.0  0.0 103256   880 pts/1    S+   18:05   0:00 grep echo.sh

0.4 使用 & 的程序

  • 程序内容:一个死循环程序,会一直打印输出
[dayuanshuai@www nohuo_test] cat echo.sh 
#! /bin/bash
i=1
while true
do
echo $i
let i++
sleep 3 
done
  • A终端使用&后台运行程序,在程序运行输出的过程中。使用ctrl+c是没有用的
[dayuanshuai@www nohuo_test] ./echo.sh &
[1] 2186
[dayuanshuai@www nohuo_test] 1
2
3
4
5
6
7
8
9
10
^C
[dayuanshuai@www nohuo_test] 11
12
13
14
^C
[dayuanshuai@www nohuo_test] 15
16
17
18
19
20
21
22
# 你会发现 程序一直打印输出,使用 `ctrl+c`完全没有任何效果
  • B终端查看后台运行状态,发现程序正在运行
[root@www ~] ps aux | grep echo.sh
500        2285  0.0  0.0 106076  1360 pts/0    S    18:42   0:00 /bin/bash ./echo.sh
root       2293  0.0  0.0 103256   880 pts/2    S+   18:42   0:00 grep echo.sh
  • 关闭A终端,再次在B终端查看程序运行状态,发现程序仍然在运行
[root@www ~] ps aux | grep echo.sh 
500        2285  0.0  0.0 106080  1404 ?        S    18:42   0:00 /bin/bash ./echo.sh
root       2451  0.0  0.0 103256   880 pts/1    S+   18:47   0:00 grep echo.sh
  • 关闭B终端后,再ssh连接上去。发现程序任然在运行
[root@www ~] ps aux | grep echo.sh
500        2285  0.0  0.0 106080  1408 ?        S    18:42   0:00 /bin/bash ./echo.sh
root       2479  0.0  0.0 103256   880 pts/1    S+   18:49   0:00 grep echo.sh
  • 那么现在只能杀死程序了。
[root@www ~] ps aux | grep echo.sh
500        2285  0.0  0.0 106080  1408 ?        S    18:42   0:00 /bin/bash ./echo.sh
root       2603  0.0  0.0 103256   880 pts/2    S+   18:51   0:00 grep echo.sh
# 使用 kill 杀死 程序
[root@www ~] kill 0 2285

# 再次查看,发现程序已经 被杀死了
[root@www ~] ps aux | grep echo.sh
root       2610  0.0  0.0 103256   876 pts/2    S+   18:51   0:00 grep echo.sh

当使用&时,前台是会显示程序运行的进程号的


0.5 使用 nohup的程序

  • A终端使用nohup命令。发现使用这个命令运行程序时,会提示:nohup: ignoring input and appending output to nohup.out'
[dayuanshuai@www nohuo_test] nohup ./echo.sh 
nohup: ignoring input and appending output to `nohup.out'
  • 使用Ctrl+c时,程序会直接退出
[dayuanshuai@www nohuo_test] nohup ./echo.sh 
nohup: ignoring input and appending output to `nohup.out'
^C
  • A终端再次运行程序,然后断开ssh连接。退出窗口。然后在B终端查看程序运行状态。发现程序还在运行
[dayuanshuai@www nohuo_test] nohup ./echo.sh 
nohup: ignoring input and appending output to `nohup.out'
[root@www ~] ps aux | grep echo.sh
500        2692  0.0  0.0 106076  1360 ?        S    19:01   0:00 /bin/bash ./echo.sh
root       2696  0.0  0.0 103256   880 pts/2    S+   19:01   0:00 grep echo.sh
  • 那么我们现在杀死程序
[root@www ~] kill -9 2692
[root@www ~] ps aux | grep echo.sh
root       2770  0.0  0.0 103256   880 pts/2    S+   19:05   0:00 grep echo.sh
  • 现在我们查看nohup.out文件。
[dayuanshuai@www ~] cat mytest/nohuo_test/nohup.out
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
*********

我们发现程序运行输出的字符全被保存在nihup.out文件里面

0.6 使用 nohup&的程序

  • 运行如下程序
[dayuanshuai@www nohuo_test] nohup ./echo.sh &  # 显示了 该程序的进程号
[1] 2858
[dayuanshuai@www nohuo_test] nohup: ignoring input and appending output to `nohup.out'  # 同时显示了nophup的信息
  • 执行ctrl+c后,查看程序是否仍然在运行
# 程序仍然在运行
[dayuanshuai@www nohuo_test] ps aux | grep echo.sh
500        2858  0.0  0.0 106076  1360 pts/0    S    21:30   0:00 /bin/bash ./echo.sh
500        2874  0.0  0.0 103256   884 pts/0    R+   21:30   0:00 grep echo.sh
  • 关闭这个终端,去别的终端上查看是否在运行
# 发现程序仍然在运行
[root@www ~] ps aux | grep echo.sh   
500        2858  0.0  0.0 106076  1364 ?        S    21:30   0:00 /bin/bash ./echo.sh
root       2962  0.0  0.0 103256   880 pts/2    S+   21:34   0:00 grep echo.sh
  • 杀死这个进程
[root@www ~] ps aux | grep echo.sh
root       2998  0.0  0.0 103256   876 pts/2    S+   21:36   0:00 grep echo.sh
  • 再次查看,发现程序已经结束
[root@www ~] ps aux | grep echo.sh
root       2998  0.0  0.0 103256   876 pts/2    S+   21:36   0:00 grep echo.sh

0.7 总结

  • &

    • 显示进程号
    • 进入后台
    • 使用ctrl+c不会导致程序的结束
    • 断开终端时,程序不会结束
  • nohup

    • 显示nohup: ignoring input and appending output to nohup.out'信息
    • 进入后台
    • ctrl+c会导致程序结束
    • 断开ssh的连接不会导致程序结束
  • nohup&

    • 显示进程号
    • 显示nohup: ignoring input and appending output to nohup.out'信息
    • CTRL+C不会导致程序结束
    • 断开终端时,程序不会结束

01. Screen命令

1.1 为什么我们要用 screen 命令?

  • 当我们在进行ssh连接执行命令。或者是用ssh传输文件时(sftpscp),或者是用telnet连接远程服务器时,我们可能会执行一些非常耗费时间的程序。然后当我们关闭窗口或者是断开连接时,这个程序就会被杀死,并没有继续执行。
  • 现在我们希望在我们关闭窗口或者是断开连接时间,这些程序不会因为连接的关闭而倒是程序也被杀死了

1.2 screen 简介

  • screen命令用于多重视窗管理程序

  • GNU Screen是一款由GNU计划开发的用于命令行终端切换的自由软件。用户可以通过该软件同时连接多个本地或远程的命令行会话,并在其间自由切换。

  • Screen可以看作是窗口管理器的命令行界面版本。它提供了统一的管理多个会话的界面和相应的功能。

  • 会话恢复

    • 只要Screen本身没有终止,在其内部运行的会话都可以恢复。这一点对于远程登录的用户特别有用——即使网络连接中断,用户也不会失去对已经打开的命令行会话的控制。只要再次登录到主机上执行screen -r就可以恢复会话的运行。同样在暂时离开的时候,也可以执行分离命令detach,在保证里面的程序正常运行的情况下让Screen挂起(切换到后台)。这一点和图形界面下的VNC很相似
  • 多窗口

    • Screen环境下,所有的会话都独立的运行,并拥有各自的编号、输入、输出和窗口缓存。用户可以通过快捷键在不同的窗口下切换,并可以自由的重定向各个窗口的输入和输出。Screen实现了基本的文本操作,如复制粘贴等;还提供了类似滚动条的功能,可以查看窗口状况的历史记录。窗口还可以被分区和命名,还可以监视后台窗口的活动。
  • 会话共享

    • Screen可以让一个或多个用户从不同终端多次登录一个会话,并共享会话的所有特性(比如可以看到完全相同的输出)。它同时提供了窗口访问权限的机制,可以对窗口进行密码保护。

1.3 Screen 命令语法与参数

  • Screen语法
screen [-AmRvx -ls -wipe][-d <作业名称>][-h <行数>][-r <作业名称>][-s <shell>][-S <作业名称>]
  • 参数说明
参数含义
-A将所有的视窗都调整为目前终端机的大小
-d<作业名称>将指定的screen作业离线。
-h<行数>指定视窗的缓冲区行数。
-m即使目前已在作业中的screen作业,仍强制建立新的screen作业
-r<作业名称>恢复离线的screen作业。
-R先试图恢复离线的作业。若找不到离线的作业,即建立新的screen作业。
-s<shell>指定建立新视窗时,所要执行的shell
-S<作业名称>指定screen作业的名称
-v显示版本信息。
-x恢复之前离线的screen作业
-ls-list显示目前所有的screen作业
-wipe检查目前所有的screen作业,并删除已经无法使用的screen作业
  • screen常用参数
命令含义
screen -S yourname新建一个叫yournamesession
screen -ls列出当前所有的session
screen -r yourname回到yourname这个session
screen -d yourname远程detach某个session
screen -d -r yourname结束当前session并回到yourname这个session
  • screen session下的的快捷键组合,在每个screen session下,所有命令都以ctrl+a(C-a) 开始
快捷键含义
Ctrl+a?显示所有键绑定信息
Ctrl+ac创建一个新的运行shell的窗口并切换到该窗口
Ctrl+anNext,切换到下一个 window
Ctrl+apPrevious,切换到前一个 Window
Ctrl+a0..9切换到第 0..9window
Ctrl+a[Space]由视窗0循序切换到视窗9
Ctrl+aCtrl+a在两个最近使用的 window 间切换
Ctrl+ax锁住当前的 window,需用用户密码解锁
Ctrl+addetach,暂时离开当前session,将目前的 screen session (可能含有多个 windows) 丢到后台执行,并会回到还没进 screen 时的状态,此时在 screen session 里,每个 window 内运行的 process (无论是前台/后台)都在继续执行,即使 logout 也不影响。
Ctrl+az把当前session放到后台执行,用 shellfg 命令则可回去。
Ctrl+aw显示所有窗口列表
Ctrl+atTime,显示当前时间,和系统的 load
Ctrl+akkillwindow,强行关闭当前的 window
Ctrl+aA为当前窗口重新命名

  • copy mode模式的快捷选项
Ctrl+a[进入copy mode,在 copy mode 下可以回滚、搜索、复制就像用使用 vi 一样
Ctrl+bBackwardPageUp
Ctrl+fForwardPageDown
H(大写) High将光标移至左上角
L Low将光标移至左下角
0移到行首
$行末
wforward one word,以字为单位往前移
bbackward one word,以字为单位往后移
Space第一次按为标记区起点,第二次按为终点
Esc结束copy mode
Ctrl+a]Paste,把刚刚在 copy mode 选定的内容贴上

1.4 Screen 命令演示

  • 安装Screen
[root@www ~]            yum -y install screen
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
# 查询是否安装成功
[root@www ~] rpm -q screen
screen-4.0.3-19.el6.x86_64
  • 启动一个screen会话
[root@www ~] screen -S cmd1
[root@www ~]# 
  • 查看当前screen窗口
[root@www ~]# screen -ls
There is a screen on:
        1756.cmd1       (Attached)
1 Socket in /var/run/screen/S-root.
  • 再新建一个窗口,然后查看screen窗口数量
[root@www ~]# screen -S myedit
[root@www ~]# screen -ls
There are screens on:
        1785.myedit     (Attached)
        1756.cmd1       (Attached)
2 Sockets in /var/run/screen/S-root.
  • ctrl+aw,查看当前所有的窗口
0*$ bash 
  • 窗口分离,将当前窗口(detach),放入后台执行,输入Ctrl+ad
[detached]
  • 重新连接会话,将后台程序调入前台
[root@www ~] screen -ls
There are screens on:
        1785.myedit     (Attached)
        1756.cmd1       (Detached)
        1810.ME (Attached)
3 Sockets in /var/run/screen/S-root.

[root@www ~] screen -r 1756
[root@www ~]# screen -ls
There are screens on:
        1785.myedit     (Attached)
        1756.cmd1       (Attached)
        1810.ME (Attached)
  • 如果所有窗口都为attach,那么我们想进入某个窗口,可以先screen -d这个窗口,再screen -r这个窗口的名字或者它的id
[root@www ~]# screen -ls
There are screens on:
        1785.myedit     (Attached)
        1756.cmd1       (Attached)
        1810.ME (Attached)
3 Sockets in /var/run/screen/S-root.

[root@www ~]# screen -d 1785

                            [root@www ~]# 
[root@www ~]# screen -ls
There are screens on:
        1785.myedit     (Detached)
        1756.cmd1       (Attached)
        1810.ME (Attached)
[root@www ~]# screen -r 1785 








[remote detached]
                                    
[root@www ~]# screen -ls
There are screens on:
        1785.myedit     (Attached)
        1756.cmd1       (Attached)
        1810.ME (Attached)

# 查看已经连接
[root@www ~]#
[root@www ~]# screen -ls
There are screens on:
        1785.myedit     (Attached)
        1756.cmd1       (Attached)
        1810.ME (Attached)
  • 继续演示
# 创建一个窗口,执行 echo_bb.sh
[root@www ~] screen -S echodigit
[root@www ~]# su - dayuanshuai
[dayuanshuai@www ~] ls
a  a.txt  compre_test  c_program  moount_test  mytest  sh_01  sh_02  sh.tar
[dayuanshuai@www ~] cd mytest/
[dayuanshuai@www mytest] cd nohuo_test/
[dayuanshuai@www nohuo_test] ls
echo_bb.sh  echo_digit.sh  echo_str.sh  nohup.out
# detached 退出这个窗口
[dayuanshuai@www nohuo_test] ./echo_bb.sh 
bb
bb
[detached]
# 再次创建一个窗口 ,执行 echo_str.sh
[root@www ~] screen -S echostr
[root@www ~]# su - dayuanshuai
# detached 退出这个窗口
[dayuanshuai@www ~] ./mytest/nohuo_test/echo_str.sh 
whoami
whoami
[detached]
# 再次创建一个窗口 执行 echo_digit.sh
[root@www ~] screen -S echobb
[root@www ~]# su - dayuanshuai
# detached 退出这个窗口
[dayuanshuai@www ~] ./mytest/nohuo_test/echo_digit.sh 
1
2

[detached]

# 查看screen窗口 运行状态
[root@www ~] screen -ls
There are screens on:
        2140.echostr    (Detached)
        2201.echobb     (Detached)
        2091.echodigit  (Detached)
3 Sockets in /var/run/screen/S-root.

# 调出 执行 echo_bb.sh的这个窗口
[root@www ~] screen -r 2091
bb
bb
bb
bb
bb
bb
bb
bb
bb
bb

# 
[root@www ~] screen -r echostr
whoami
whoami
whoami
whoami
whoami
whoami
whoami
whoami
whoami
whoami
whoami
whoami

[root@www ~] screen -ls
There are screens on:
        2140.echostr    (Detached)
        2201.echobb     (Detached)
        2091.echodigit  (Detached)
3 Sockets in /var/run/screen/S-root.

# 调出 echostr 这个窗口
[root@www ~] screen -r echostr
whoami
whoami
whoami
whoami
whoami
whoami
whoami
whoami


# 查看运行状态,再调出 2201 这个窗口
[root@www ~] screen -r 2201
174
175
176
177
178
179
180
181
182
183
184
185
186

[root@www ~] screen -ls
There are screens on:
        2140.echostr    (Detached)
        2201.echobb     (Detached)
        2091.echodigit  (Detached)
3 Sockets in /var/run/screen/S-root.

# 再调出一次发现其已经执行到 216了,说明程序一直在后台运行
[root@www ~] screen -r 2201
208
209
210
211
212
213
214
215
216
  • 关闭窗口
    • ctrl+ak
    • kill

1.5 screen 其他操作

  • 屏幕共享
    • 当用户在一个终端里面开启一个screen会话窗口,那么以同样一个用户登录的终端就可以使用screen -x可以实时看到另外一个终端的实时操作
# 开启一个 screen 会话
[root@www ~]     screen -S mytest
[root@www ~]# whoself
bash: whoself: command not found
[root@www ~]# whoami
root
[root@www ~]# howareyou
bash: howareyou: command not found
[root@www ~]# hihi

# 另外一个终端执行 screen -x 直接就可以看到 另外一个终端的实时输入
[root@www ~] screen -x
[root@www ~]# whoself
bash: whoself: command not found
[root@www ~]# whoami
root
[root@www ~]# howareyou
bash: howareyou: command not found
[root@www ~]# hihi  
  • 屏幕分割

    • Ctrl+aS:水平分割
    • Ctrl+aX:关闭当前screen窗口
    • Ctrl+aQ:关闭其他screen窗口
  • C/P操作

    • CTRL+a[可以进入copy/paste模式,这个模式下可以像在vi中一样移动光标,并可以使用空格键设置标记。其实在这个模式下有很多类似vi的操作,譬如使用/进行搜索,使用y快速标记一行,使用w快速标记一个单词等。关于C/P模式下的高级操作,其文档的这一部分有比较详细的说明。
    • 一般情况下,可以移动光标到指定位置,按下空格设置一个开头标记,然后移动光标到结尾位置,按下空格设置第二个标记,同时会将两个标记之间的部分储存在copy/paste buffer中,并退出copy/paste模式。在正常模式下,可以使用快捷键C-a ]将储存在buffer中的内容粘贴到当前窗口。
  • screen配置文件

    • /etc/screenrc
    • $HOME/.screenrc
    • screen默认是以单用户模式运行的,你需要在配置文件中指定multiuser on来打开多用户模式,通过acl*(acladd,acldel,aclchg...)命令,你可以灵活配置其他用户访问你的screen会话

写在最后的话:

  • 无论每个知识点的难易程度如何,我都会尽力将它描绘得足够细致
  • 欢迎关注我的CSDN博客,IDYS’BLOG
  • 持续更新内容
    linux基础 | 数据通信(路由交换,WLAN) | Python基础 | 云计算
  • 如果你有什么疑问,或者是难题。欢迎评论或者私信我。你若留言,我必回复!
  • 虽然我现在还很渺小,但我会做好每一篇内容。谢谢关注!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值