在Linux和Docker中使用nohup进行后台任务管理

在Linux和Docker中使用nohup进行后台任务管理

一、nohup应用场景

在Linux操作系统(Deepin、Ubuntu)中,经常需要将程序长时间运行,不希望终端关闭时程序关闭,这时候可以使用nohup命令来实现在命令行终端运行后台程序的目的。

另外,在docker环境下运行脚本时,常常将运行程序放在后台,保持前台交互,更是需要频繁使用nohup命令。

二、nohup命令

nohup 英文全称 no hang up(不挂起),用于在系统后台不挂断地运行命令,退出终端不会影响程序的运行。

nohup 命令,在默认情况下(非重定向时),会在当前目录输出一个名叫 nohup.out 的日志文件,如果当前目录的 nohup.out 文件不可写,输出重定向到 $HOME/nohup.out 文件中。

语法格式

复制代码
 nohup Command [ Arg ... ] [ & ]

参数说明:

Command:要执行的命令。

Arg:一些参数,可以指定输出文件。

&:让命令在后台执行,终端退出后命令仍旧执行。

示例

  • 编辑一个延时60秒的sh文件delay.sh,sh文件内容如下

    shell 复制代码
    #!/bin/bash
    
    # 这是一个等待1分钟的脚本
    echo "开始等待..."
    
    # 使用sleep命令等待60秒
    sleep 60
    
    # 等待结束后执行的代码
    echo "等待结束!"
  • 使用nohup进行后台运行

    shell 复制代码
    # 运行3个delay.sh,其中2、3输出日志到指定文件logdelay.txt
    deepindemo@deepindemo:~/Downloads$ nohup ./delay.sh &
    [1] 838103
    deepindemo@deepindemo:~/Downloads$ nohup ./delay.sh > logdelay.txt 2>&1 &
    [2] 838108
    deepindemo@deepindemo:~/Downloads$ nohup ./delay.sh > logdelay.txt 2>&1 &
    [3] 838111
    # 使用&执行一次delay.sh的后台运行,在开始等待...后输入回车进入交互界面
    deepindemo@deepindemo:~/Downloads$ ./delay.sh &
    [4] 838113
    deepindemo@deepindemo:~/Downloads$ 开始等待...
    
    deepindemo@deepindemo:~/Downloads$

三、后台任务管理

3.1 列出后台任务jobs

jobs命令用于显示当前shell环境中的任务列表及任务状态,包括后台运行的任务。这些任务可以是正在运行的,也可以是被挂起的。

当执行一个命令并在其后加上&符号时,该命令将在后台运行。

例如,sleep 10 &将在后台启动一个休眠10秒的进程。在这个进程中,可以执行其他命令,而该后台进程将在指定的时间后完成。

jobs命令可以显示这些后台任务的状态。任务的状态可以是running(正在运行)、stopped(被挂起)或Terminated(已终止)。对于每个任务,jobs还会显示一个任务号,可以使用这个任务号来引用特定的任务。

jobs命令其语法结构如下:

bash 复制代码
jobs [-l] [-n] [-p] [-r] [-s] [%job_spec ...]

这里的选项和参数的含义如下:

  • 选项:

    • -l:列出任务的进程ID(PID)。
    • -n:仅显示任务状态变化的信息。
    • -p:仅显示每个任务的进程ID。
    • -r:仅显示运行状态(running)的任务。
    • -s:仅显示停止状态(stopped)的任务。
  • 参数:

    • %job_spec:用于指定要显示的任务的标识号。你可以使用任务号或者命令名的一部分来匹配并显示特定的任务。如果省略此参数,jobs将显示所有后台任务。

示例:

  1. jobs:显示当前所有后台任务。

    shell 复制代码
    deepindemo@deepindemo:~/Downloads$ jobs
    [1]   Running                 nohup ./delay.sh > logdelay.txt 2>&1 &
    [2]   Running                 nohup ./delay.sh > logdelay.txt 2>&1 &
    [3]-  Running                 nohup ./delay.sh > logdelay.txt 2>&1 &
  2. jobs -l:显示当前系统中所有后台任务及其进程ID。

    shell 复制代码
    deepindemo@deepindemo:~/Downloads$ jobs -l
    [1]  838117 Running                 nohup ./delay.sh > logdelay.txt 2>&1 &
    [2]  838119 Running                 nohup ./delay.sh > logdelay.txt 2>&1 &
    [3]- 838121 Running                 nohup ./delay.sh > logdelay.txt 2>&1 &
  3. jobs -r:仅显示正在运行的后台任务。

    shell 复制代码
    deepindemo@deepindemo:~/Downloads$ jobs -r
    [1]   Running                 nohup ./delay.sh > logdelay.txt 2>&1 &
    [2]   Running                 nohup ./delay.sh > logdelay.txt 2>&1 &
    [3]-  Running                 nohup ./delay.sh > logdelay.txt 2>&1 &
  4. jobs -s:仅显示已停止的后台任务。

    shell 复制代码
    deepindemo@deepindemo:~/Downloads$ jobs -s
    deepindemo@deepindemo:~/Downloads$
  5. jobs -p:仅显示每个后台任务的进程ID。

    shell 复制代码
    deepindemo@deepindemo:~/Downloads$ jobs -p
    838117
    838119
    838121
  6. jobs %1:显示任务号为1的后台任务。

    shell 复制代码
    deepindemo@deepindemo:~/Downloads$ jobs %1
    [1]   Running                 nohup ./delay.sh > logdelay.txt 2>&1 &

3.2 后台任务转前台fg

可以使用 fg 命令将后台任务转到前台任务。fg 命令用于将作业从后台移至前台,并恢复其执行。

将后台任务转到前台,按照以下步骤进行操作:

  1. 首先使用 jobs 命令查看当前在后台运行的任务列表。这将显示任务的状态和作业号。

    bash 复制代码
    jobs
  2. 记住想要转到前台的任务的作业号。作业号通常位于任务列表的前面,并且带有 + 符号表示最近放置到后台的任务。

  3. 使用 fg 命令,并在其后跟上作业号,将后台任务转到前台。

    bash 复制代码
    fg %作业号

    或者,如果作业号是最近放置到后台的任务,你可以只使用 fg 命令,不带作业号。

    bash 复制代码
    fg
  4. 注意
    如果将停止状态(stopped)的后台任务转前台,需要先使用 bg 命令将其在后台继续执行,然后再使用 fg 命令将其转到前台。

    bash 复制代码
    bg %作业号  # 将任务放到后台继续执行
    fg %作业号  # 将任务转到前台

3.3 删除后台任务kill

kill命令用于向进程发送信号,默认情况下是终止进程的执行,包括后台和前台进程。

删除后台任务,可以按照以下步骤进行操作:

  1. 首先使用 jobs 命令查看当前在后台运行的任务列表。显示任务的状态和作业号。

    bash 复制代码
    deepindemo@deepindemo:~/Downloads$ jobs -l
    [1]  838132 Running                 nohup ./delay.sh > logdelay.txt 2>&1 &
    [2]  838134 Running                 nohup ./delay.sh > logdelay.txt 2>&1 &
    [3]- 838136 Running                 nohup ./delay.sh > logdelay.txt 2>&1 &
    [4]+ 838138 Running                 nohup ./delay.sh > logdelay.txt 2>&1 &
  2. 记住想要删除的后台任务的PID。

  3. 使用 kill 命令,并在其后跟上任务PID,可以一次删除一个或多个。

    bash 复制代码
    deepindemo@deepindemo:~/Downloads$ kill 838132
    deepindemo@deepindemo:~/Downloads$ kill 838134 838136 838138
    [1]   Terminated              nohup ./delay.sh > logdelay.txt 2>&1
  4. 使用 jobs 命令查询后台任务状态

    bash 复制代码
    deepindemo@deepindemo:~/Downloads$ jobs
    [2]   Terminated              nohup ./delay.sh > logdelay.txt 2>&1
    [3]-  Terminated              nohup ./delay.sh > logdelay.txt 2>&1
    [4]+  Terminated              nohup ./delay.sh > logdelay.txt 2>&1
  5. 再次使用 jobs 命令查询后台任务状态,已经显示Terminated信息的后台任务不会再出现

    bash 复制代码
    deepindemo@deepindemo:~/Downloads$ jobs
    deepindemo@deepindemo:~/Downloads$

may the odds be ever in your favor ~

相关推荐
程序员-Benothing3 分钟前
Linux性能分析方法论:USE、RED、火焰图与perf实战
linux·运维·服务器
问心无愧05138 分钟前
[ACTF2020 新生赛]Exec
运维·服务器·笔记
cg.family11 分钟前
K8S:scale 与 delete pod 核心区别
运维·docker·容器
开开心心_Every14 分钟前
黄金价格软件国内外价格实时监控超方便
运维·服务器·游戏·智能手机·pdf·lua·perl
云中双月15 分钟前
瑞芯微rv1106进入Loader模式转储emmc数据
linux·嵌入式·uboot·u-boot·emmc·rockchip·烧录·rv1106·瑞芯微
yunwei3715 分钟前
eBPF 入门实践教程:BPF Token,安全的委托式权限与程序加载
linux·后端·性能优化
Initialize2930815 分钟前
systemd 服务起不来?五步排查法
运维
LMT_Lanmantech16 分钟前
HyperWorks许可:almutil显示Unit满了,数据到底准不准?
运维·服务器·算法
system_error_x18 分钟前
服务器的替代方案:从物理机到云原生的全面对比
运维·服务器·云原生
不知语冰18 分钟前
达妙 Orin NX V2:JetPack 安装中文拼音输入法
linux