• linux:将进程切换到后台且不退出


    前言

    • 如果需要在后台执行且关闭shell不退出,在程序启动命令中加入 &
    • 如果开始没有加入 &,发现命令执行时间很长,可以参考本文

    将进程切换到后台且不退出

    1. 将进程切换到后台(Ctrl + Z
    2. 让进程在后台继续执行(bg x
    3. 使用exit命令退出当前shell,进程不退出

    将进程暂停且切换到后台

    使用Ctrl + Z将命令暂停切换到后台。Ctrl + Z 是将任务中止(暂停的意思),但是此任务并没有结束,他仍然在进程中他只是维持挂起的状态

    shell> wget https://download.filezilla-project.org/client/FileZilla_3.60.2_win64_sponsored-setup.exe
    --2022-08-06 10:56:21--  https://download.filezilla-project.org/client/FileZilla_3.60.2_win64_sponsored-setup.exe
    Resolving download.filezilla-project.org (download.filezilla-project.org)... 49.12.121.47, 2a01:4f8:242:52d0::2
    Connecting to download.filezilla-project.org (download.filezilla-project.org)|49.12.121.47|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 11854128 (11M) [application/x-msdos-program]
    Saving to: ‘FileZilla_3.60.2_win64_sponsored-setup.exe.2’
    
     1% [>                                                                                   ] 122,880     30.2KB/s  eta 6m 19s 
    ^Z (输入Ctrl + Z)
    [1]+  Stopped                 wget https://download.filezilla-project.org/client/FileZilla_3.60.2_win64_sponsored-setup.exe
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    查看后台的进程

    shell> jobs -l
    [1]+  Stopped                 wget https://download.filezilla-project.org/client/FileZilla_3.60.2_win64_sponsored-setup.exe
    
    • 1
    • 2
    • 后台有1个已暂停的程序
    • [1]表示进程在后台的编号为1

    将进程切换到前台且继续执行

    shell> fg 1
    wget https://download.filezilla-project.org/client/FileZilla_3.60.2_win64_sponsored-setup.exe
     3% [====>                                                                                                                                                               ] 409,600      992B/s   in 6m 53s  
    
    2022-08-06 11:03:16 (992 B/s) - Connection closed at byte 409600. Retrying.
    
    --2022-08-06 11:03:17--  (try: 2)  https://download.filezilla-project.org/client/FileZilla_3.60.2_win64_sponsored-setup.exe
    Connecting to download.filezilla-project.org (download.filezilla-project.org)|49.12.121.47|:443... connected.
    HTTP request sent, awaiting response... 206 Partial Content
    Length: 11854128 (11M), 11444528 (11M) remaining [application/x-msdos-program]
    Saving to: ‘FileZilla_3.60.2_win64_sponsored-setup.exe.2’
    
     7% [+++++=====>                                                                                                                                                         ] 860,160     28.1KB/s  eta 5m 16s
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • fg 1中的1为进程在后台的编号

    让进程在后台继续执行

    shell> bg 1
    [1]+ wget https://download.filezilla-project.org/client/FileZilla_3.60.2_win64_sponsored-setup.exe &
     1% [>                                                                                                                                                                   ] 122,880      534B/s  eta 6h 5m   [ 
     5% [=======>                                                                                                                                                            ] 630,784     19.6KB/s  eta 73m 5s 
    
    • 1
    • 2
    • 3
    • 4
    • bg 1中的1为进程在后台的编号

    查看后台的进程:

    [1]+  Running                 wget https://download.filezilla-project.org/client/FileZilla_3.60.2_win64_sponsored-setup.exe &
    
    • 1
    • 执行 bg 1后,系统在命令后面加入&符号
    • 看到&符号后,此时可以通过exit命令退出当前shell。执行exit退出shell后,进程不退出。
    • 另一shell,使用ps -ef|grep wget可查看到在后台运行的进程。
  • 相关阅读:
    Spring&SpringMVC&SpringBoor
    arm64-v8a和armeabi-v7a分别是什么?它们之间有什么区别
    图神经网络基础
    半导体工厂将应用哪些制造创新技术?
    华为推出最速超级充电桩 号称1秒1公里 | 百能云芯
    SpringBoot+Vue 的网上图书商城管理系统
    Electron[3] 基础配置准备和Electron入门案例
    Laya---横向滚动列表
    深度学习实践3:多层感知机
    如何在windows上 安装&更新 显卡的驱动
  • 原文地址:https://blog.csdn.net/sayyy/article/details/126191261