博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux 后台执行命令
阅读量:4362 次
发布时间:2019-06-07

本文共 4373 字,大约阅读时间需要 14 分钟。

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

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

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

[root@VM_139_74_centos shell]# ./10s.shWed May 30 11:32:13 CST 2018Wed May 30 11:32:14 CST 2018Wed May 30 11:32:15 CST 2018Wed May 30 11:32:16 CST 2018Wed May 30 11:32:17 CST 2018Wed May 30 11:32:18 CST 2018Wed May 30 11:32:19 CST 2018Wed May 30 11:32:20 CST 2018Wed May 30 11:32:21 CST 2018Wed 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 2018Wed May 30 11:32:31 CST 2018Wed May 30 11:32:32 CST 2018Wed May 30 11:32:33 CST 2018Wed May 30 11:32:34 CST 2018Wed May 30 11:32:35 CST 2018Wed May 30 11:32:36 CST 2018Wed May 30 11:32:37 CST 2018Wed May 30 11:32:38 CST 2018Wed May 30 11:32:39 CST 2018nohup ./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 inputWed May 30 11:38:36 CST 2018Wed May 30 11:38:37 CST 2018Wed May 30 11:38:38 CST 2018Wed May 30 11:38:39 CST 2018Wed May 30 11:38:40 CST 2018Wed May 30 11:38:41 CST 2018Wed 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 2018Wed 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 2018Wed 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.shWed May 30 11:51:31 CST 2018Wed 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 2018Wed May 30 11:53:40 CST 2018jobs[1]+  Running                 ./10s.sh &[root@VM_139_74_centos shell]# Wed May 30 11:53:41 CST 2018Wed May 30 11:53:42 CST 2018Wed May 30 11:53:43 CST 2018Wed 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 CMD17113 pts/0    00:00:00 bash20062 pts/0    00:00:00 10s.sh20064 pts/0    00:00:00 sleep20074 pts/0    00:00:00 ps[root@VM_139_74_centos shell]# kill 20062[root@VM_139_74_centos shell]# ps  PID TTY          TIME CMD17113 pts/0    00:00:00 bash20087 pts/0    00:00:00 ps[1]+  Terminated              nohup ./10s.sh

转载于:https://www.cnblogs.com/kika/p/10851596.html

你可能感兴趣的文章
从垃圾回收看闭包
查看>>
Intel Core Microarchitecture Pipeline
查看>>
如何去除交叉表的子行(列)的小计?
查看>>
Web字体(链接)嵌入
查看>>
switch… case 语句的用法
查看>>
day07补充-数据类型总结及拷贝
查看>>
语言、数据和运算符
查看>>
正则表达式30分钟入门教程
查看>>
sqlserver try catch·
查看>>
怎么在三维世界里叙述五维故事
查看>>
1028: 可乐(2018年中南大学研究生复试机试题 )
查看>>
珍藏的最全的windows操作系统快捷键
查看>>
【DBAplus】SQL优化:一篇文章说清楚Oracle Hint的正确使用姿势
查看>>
二叉树结点删除操作
查看>>
图论-单源最短路-SPFA算法
查看>>
转换文件的字符集
查看>>
软件质量理解
查看>>
jquery 在 table 中修改某行值
查看>>
pyc文件是什么【转载】
查看>>
POM.xml 标签详解
查看>>