• Linux 后台执行命令


    下面的示例统一使用这个每秒打印一次时间的简单脚本:

    [root@VM_139_74_centos shell]# cat 10s.sh
    #!/bin/bash
    
    for ((i = 0; i < 10; i++)); do
        echo $(date)
        sleep 1
    done

    正常执行命令时,会阻塞终端,同时将所有输出信息打印到终端:

    [root@VM_139_74_centos shell]# ./10s.sh
    Wed May 30 11:32:13 CST 2018
    Wed May 30 11:32:14 CST 2018
    Wed May 30 11:32:15 CST 2018
    Wed May 30 11:32:16 CST 2018
    Wed May 30 11:32:17 CST 2018
    Wed May 30 11:32:18 CST 2018
    Wed May 30 11:32:19 CST 2018
    Wed May 30 11:32:20 CST 2018
    Wed May 30 11:32:21 CST 2018
    Wed May 30 11:32:22 CST 2018
    [root@VM_139_74_centos shell]#

    注意,Linux 终端中执行的命令的输出是可以有缓存的。有时候命令执行完了,但是输出的信息并不完整,这时候再需要敲其他命令(例如 Ctrl + c)清空缓存。

    &

    命令后面有 & 时,这个命令将会在后台运行。但是输出信息仍会打印到终端,但是此时可以接受其他命令:

    [root@VM_139_74_centos shell]# ./10s.sh &
    [1] 17257
    [root@VM_139_74_centos shell]# Wed May 30 11:32:30 CST 2018
    Wed May 30 11:32:31 CST 2018
    Wed May 30 11:32:32 CST 2018
    Wed May 30 11:32:33 CST 2018
    Wed May 30 11:32:34 CST 2018
    Wed May 30 11:32:35 CST 2018
    Wed May 30 11:32:36 CST 2018
    Wed May 30 11:32:37 CST 2018
    Wed May 30 11:32:38 CST 2018
    Wed May 30 11:32:39 CST 2018
    nohup ./10s.sh &
    [2] 17290
    [1]   Done                    ./10s.sh
    [root@VM_139_74_centos shell]# nohup: ignoring input and appending output to ‘nohup.out’

    & 特点如下:

    • 终端关闭或退出账户时,后台运行的命令会终止
    • 后台运行的作业一样会将结果输出到屏幕上

    对于第一个问题,可以在命令前添加 nohup(no hang up)解决:

    nohup your-command &

    第二个问题可以使用输出重定向将输出重定向到文件:

    your-command > out.file 2>&1  & 
    

    其中,your-command > out.file 将命令的标准输出流 STDOUT 重定向到 out.file 文件,不再打印到屏幕上。2>&1 将标准错误流 STDERR 重定向到标准输出流(2 与 > 结合表示重定向标准错误流 STDERR,&1 表示标准输出流 STDOUT),而标准输出流已经重定向到了 out.file 文件,所以标准错误流也会输出到这个文件中。最后的 & 表示该命令在后台执行。

    nohup

    使用 nohup 命令时,如果没有指定输出文件,此时命令的所有输出都被重定向到一个名为 nohup.out 的文件中。

    [root@VM_139_74_centos shell]# nohup ./10s.sh &
    [1] 17816
    [root@VM_139_74_centos shell]# nohup: ignoring input and appending output to ‘nohup.out’
    nohup ./60s.sh^C
    [1]+  Done                    nohup ./10s.sh

    也可以自己指定输出重定向到的文件:

    [root@VM_139_74_centos shell]# nohup ./10s.sh > myout.file 2>&1 &
    [1] 17944
    [root@VM_139_74_centos shell]#
    [root@VM_139_74_centos shell]# cat myout.file 
    nohup: ignoring input
    Wed May 30 11:38:36 CST 2018
    Wed May 30 11:38:37 CST 2018
    Wed May 30 11:38:38 CST 2018
    Wed May 30 11:38:39 CST 2018
    Wed May 30 11:38:40 CST 2018
    Wed May 30 11:38:41 CST 2018
    Wed May 30 11:38:42 CST 2018

    注意,使用 nohup 后台运行命令时,需要使用 exit 正常退出当前账户,这样才能保证命令一直在后台运行。

    ctrl + z

    挂起前台进程。将正在前台执行的命令放到后台,并且处于暂停状态。

    [root@VM_139_74_centos shell]# ./10s.sh 
    Wed May 30 11:44:52 CST 2018
    Wed May 30 11:44:53 CST 2018
    ^Z
    [1]+  Stopped                 ./10s.sh
    [root@VM_139_74_centos shell]# jobs
    [1]+  Stopped                 ./10s.sh

    jobs

    查看所有后台运行的命令。

    [root@VM_139_74_centos shell]# nohup ./10s.sh > myout.file 2>&1 &
    [2] 18573
    [1]   Done                    nohup ./10s.sh > myout.file 2>&1
    [root@VM_139_74_centos shell]# jobs
    [2]+  Running                 nohup ./10s.sh > myout.file 2>&1 &

    fg

    将后台命令调至前台,并恢复运行。

    如果后台中有多个命令,可以用 fg %jobnumber 将选中的命令调出,%jobnumber 是通过 jobs 命令查到的后台正在执行的命令的序号(不是 pid)。

    [root@VM_139_74_centos shell]# ./10s.sh 
    Wed May 30 11:44:52 CST 2018
    Wed May 30 11:44:53 CST 2018
    ^Z
    [1]+  Stopped                 ./10s.sh
    [root@VM_139_74_centos shell]# jobs
    [1]+  Stopped                 ./10s.sh
    [root@VM_139_74_centos shell]# fg 1
    ./10s.sh
    Wed May 30 11:51:31 CST 2018
    Wed May 30 11:51:32 CST 2018
    ^Z
    [1]+  Stopped                 ./10s.sh
    [root@VM_139_74_centos shell]#

    bg

    恢复执行后台暂停的命令。

    如果后台中有多个命令,可以用 bg %jobnumber 将选中的命令调出,%jobnumber 是通过 jobs 命令查到的后台正在执行的命令的序号(不是 pid)。

    [root@VM_139_74_centos shell]# jobs
    [1]+  Stopped                 ./10s.sh
    [root@VM_139_74_centos shell]# bg
    [1]+ ./10s.sh &
    [root@VM_139_74_centos shell]# Wed May 30 11:53:39 CST 2018
    Wed May 30 11:53:40 CST 2018
    jobs
    [1]+  Running                 ./10s.sh &
    [root@VM_139_74_centos shell]# Wed May 30 11:53:41 CST 2018
    Wed May 30 11:53:42 CST 2018
    Wed May 30 11:53:43 CST 2018
    Wed May 30 11:53:44 CST 2018
    ^C
    [1]+  Done                    ./10s.sh
    [root@VM_139_74_centos shell]# 

    kill

    杀死进程。需要使用通过 ps 命令看到的进程 PID。

    [root@VM_139_74_centos shell]# nohup ./10s.sh &
    [1] 20062
    [root@VM_139_74_centos shell]# nohup: ignoring input and appending output to ‘nohup.out’
    ps
      PID TTY          TIME CMD
    17113 pts/0    00:00:00 bash
    20062 pts/0    00:00:00 10s.sh
    20064 pts/0    00:00:00 sleep
    20074 pts/0    00:00:00 ps
    [root@VM_139_74_centos shell]# kill 20062
    [root@VM_139_74_centos shell]# ps
      PID TTY          TIME CMD
    17113 pts/0    00:00:00 bash
    20087 pts/0    00:00:00 ps
    [1]+  Terminated              nohup ./10s.sh
  • 相关阅读:
    Python爬虫入门一之综述
    关于 PHP 7 你必须知道的五件事
    10个用于Web开发的最好 Python 框架
    如何用Python编写一个聊天室
    无需操作系统直接运行 Python 代码
    使用Python开发chrome插件
    Python下用Scrapy和MongoDB构建爬虫系统(1)
    python 目录操作
    用主题模型可视化分析911新闻(Python版)
    React Native通信机制详解
  • 原文地址:https://www.cnblogs.com/kika/p/10851596.html
Copyright © 2020-2023  润新知