|
| 1 | +disown |
| 2 | +=== |
| 3 | + |
| 4 | +从当前的shell中移除作业。 |
| 5 | + |
| 6 | +## 概要 |
| 7 | + |
| 8 | +```shell |
| 9 | +disown [-h] [-ar] [jobspec ... | pid ...] |
| 10 | +``` |
| 11 | + |
| 12 | +## 主要用途 |
| 13 | + |
| 14 | +- 从当前shell的作业列表中移除全部作业。 |
| 15 | + |
| 16 | +- 从当前shell的作业列表中移除指定的一到多个作业。 |
| 17 | + |
| 18 | +- 从当前shell的作业列表中移除正在运行的作业。 |
| 19 | + |
| 20 | +- 标记作业,使得它们在当前shell退出后也不会结束。 |
| 21 | + |
| 22 | + |
| 23 | +## 选项 |
| 24 | + |
| 25 | +```shell |
| 26 | +-h 标记每个作业标识符,这些作业将不会在shell接收到sighup信号时接收到sighup信号。 |
| 27 | +-a 移除所有的作业。 |
| 28 | +-r 移除运行的作业。 |
| 29 | +``` |
| 30 | + |
| 31 | +## 参数 |
| 32 | + |
| 33 | +jobspec(可选):要移除的作业标识符,可以是一到多个。 |
| 34 | + |
| 35 | +pid(可选):要移除的作业对应的进程ID,可以是一到多个。 |
| 36 | + |
| 37 | + |
| 38 | +## 返回值 |
| 39 | + |
| 40 | +返回成功除非未开启作业控制或执行出现错误。 |
| 41 | + |
| 42 | +## 例子 |
| 43 | + |
| 44 | +```shell |
| 45 | +# 演示。 |
| 46 | +[user2@pc] ssh 192.168.1.4 |
| 47 | +user2@192.168.1.4's password: |
| 48 | +# 此时按下ctrl+z使得交互停止。 |
| 49 | +[1]+ Stopped ssh 192.168.1.4 |
| 50 | +
|
| 51 | +[user2@pc] ssh 192.168.1.7 |
| 52 | +user2@192.168.1.7's password: |
| 53 | +# 此时按下ctrl+z使得交互停止。 |
| 54 | +[1]+ Stopped ssh 192.168.1.7 |
| 55 | + |
| 56 | +[user2@pc] sleep 120 & |
| 57 | +[3] 28986 |
| 58 | + |
| 59 | +# 列出作业及pid信息。 |
| 60 | +[user2@pc] jobs -l |
| 61 | +[1]- 28756 Stopped ssh 192.168.1.4 |
| 62 | +[2]+ 28833 Stopped ssh 192.168.1.7 |
| 63 | +[3] 28986 Running sleep 120 & |
| 64 | + |
| 65 | +# 删除运行状态的作业。 |
| 66 | +[user2@pc] disown -r |
| 67 | + |
| 68 | +[user2@pc] jobs -l |
| 69 | +[1]- 28756 Stopped ssh 192.168.1.4 |
| 70 | +[2]+ 28833 Stopped ssh 192.168.1.7 |
| 71 | + |
| 72 | +# 注意disown只是移除作业,并没有停止。 |
| 73 | +[user2@pc] pgrep -a -u user2 -f 'sleep 120' |
| 74 | +28986 sleep 120 |
| 75 | + |
| 76 | +# 删除指定的作业。 |
| 77 | +[user2@pc] disown %2 |
| 78 | +bash: warning: deleting stopped job 2 with process group 28833 |
| 79 | + |
| 80 | +[user2@pc] jobs -l |
| 81 | +[1]- 28756 Stopped ssh 192.168.1.4 |
| 82 | + |
| 83 | +# 注意disown只是移除作业,并没有停止。 |
| 84 | +[user2@pc] pgrep -a -u user2 -f 'ssh 192.168.1.7' |
| 85 | +28833 ssh 192.168.1.7 |
| 86 | + |
| 87 | +# 删除全部作业。 |
| 88 | +[user2@pc] disown -a |
| 89 | +bash: warning: deleting stopped job 1 with process group 28756 |
| 90 | + |
| 91 | +[user2@pc] jobs -l |
| 92 | + |
| 93 | +# 注意disown只是移除作业,并没有停止。 |
| 94 | +[user2@pc] pgrep -a -u user2 -f 'ssh 192.168.1.4' |
| 95 | +28756 ssh 192.168.1.4 |
| 96 | +``` |
| 97 | + |
| 98 | +```shell |
| 99 | +# 演示-h选项的作用。 |
| 100 | +[user2@pc] sleep 90 & |
| 101 | +[1] 109080 |
| 102 | + |
| 103 | +[user2@pc] jobs -l |
| 104 | +[1]+ 109080 Running sleep 90 & |
| 105 | + |
| 106 | +[user2@pc] disown -h %1 |
| 107 | + |
| 108 | +[user2@pc] exit |
| 109 | + |
| 110 | +# 此时前一个终端已经关闭,现在打开新终端查找该作业。 |
| 111 | +[user2@pc] pgrep -a -u user2 -f 'sleep 90' |
| 112 | +109080 sleep 90 |
| 113 | +``` |
| 114 | + |
| 115 | +### 注意 |
| 116 | + |
| 117 | +1. `bash`的作业控制命令包括`bg fg kill wait disown suspend`。 |
| 118 | +2. 该命令需要`set`选项`monitor`处于开启状态时才能执行;查看作业控制状态:输入`set -o`查看`monitor`行;执行`set -o monitor`或`set -m`开启该选项。 |
| 119 | +3. 该命令是bash内建命令,相关的帮助信息请查看`help`命令。 |
| 120 | + |
| 121 | +### 参考链接 |
| 122 | + |
| 123 | +- [disown的用法](https://www.cyberciti.biz/faq/unix-linux-disown-command-examples-usage-syntax/) |
| 124 | + |
| 125 | +<!-- Linux命令行搜索引擎:https://jaywcjlove.github.io/linux-command/ --> |
0 commit comments