Linux命令 disown - 從當(dāng)前的shell中移除作業(yè)

2021-11-08 15:13 更新

disown

從當(dāng)前的shell中移除作業(yè)。

概要

disown [-h] [-ar] [jobspec ... | pid ...]

主要用途

  • 從當(dāng)前shell的作業(yè)列表中移除全部作業(yè)。
  • 從當(dāng)前shell的作業(yè)列表中移除指定的一到多個(gè)作業(yè)。
  • 從當(dāng)前shell的作業(yè)列表中移除正在運(yùn)行的作業(yè)。
  • 標(biāo)記作業(yè),使得它們?cè)诋?dāng)前shell退出后也不會(huì)結(jié)束。

選項(xiàng)

-h    標(biāo)記每個(gè)作業(yè)標(biāo)識(shí)符,這些作業(yè)將不會(huì)在shell接收到sighup信號(hào)時(shí)接收到sighup信號(hào)。
-a    移除所有的作業(yè)。
-r    移除運(yùn)行的作業(yè)。

參數(shù)

jobspec(可選):要移除的作業(yè)標(biāo)識(shí)符,可以是一到多個(gè)。

pid(可選):要移除的作業(yè)對(duì)應(yīng)的進(jìn)程ID,可以是一到多個(gè)。

返回值

返回成功除非未開啟作業(yè)控制或執(zhí)行出現(xiàn)錯(cuò)誤。

例子

# 演示。
[user2@pc] ssh 192.168.1.4
user2@192.168.1.4's password:
# 此時(shí)按下ctrl+z使得交互停止。
[1]+  Stopped                 ssh 192.168.1.4

[user2@pc] ssh 192.168.1.7
user2@192.168.1.7's password:
# 此時(shí)按下ctrl+z使得交互停止。
[1]+  Stopped                 ssh 192.168.1.7

[user2@pc] sleep 120 &
[3] 28986

# 列出作業(yè)及pid信息。
[user2@pc] jobs -l
[1]- 28756 Stopped                 ssh 192.168.1.4
[2]+ 28833 Stopped                 ssh 192.168.1.7
[3]  28986 Running                 sleep 120 &

# 刪除運(yùn)行狀態(tài)的作業(yè)。
[user2@pc] disown -r

[user2@pc] jobs -l
[1]- 28756 Stopped                 ssh 192.168.1.4
[2]+ 28833 Stopped                 ssh 192.168.1.7

# 注意disown只是移除作業(yè),并沒有停止。
[user2@pc] pgrep -a -u user2 -f 'sleep 120'
28986 sleep 120

# 刪除指定的作業(yè)。
[user2@pc] disown %2
bash: warning: deleting stopped job 2 with process group 28833

[user2@pc] jobs -l
[1]- 28756 Stopped                 ssh 192.168.1.4

# 注意disown只是移除作業(yè),并沒有停止。
[user2@pc] pgrep -a -u user2 -f 'ssh 192.168.1.7'
28833 ssh 192.168.1.7

# 刪除全部作業(yè)。
[user2@pc] disown -a
bash: warning: deleting stopped job 1 with process group 28756

[user2@pc] jobs -l

# 注意disown只是移除作業(yè),并沒有停止。
[user2@pc] pgrep -a -u user2 -f 'ssh 192.168.1.4'
28756 ssh 192.168.1.4
# 演示-h選項(xiàng)的作用。
[user2@pc] sleep 90 &
[1] 109080

[user2@pc] jobs -l
[1]+ 109080 Running                 sleep 90 &

[user2@pc] disown -h %1

[user2@pc] exit

# 此時(shí)前一個(gè)終端已經(jīng)關(guān)閉,現(xiàn)在打開新終端查找該作業(yè)。
[user2@pc] pgrep -a -u user2 -f 'sleep 90'
109080 sleep 90

注意

  1. bash的作業(yè)控制命令包括bg fg kill wait disown suspend。
  2. 該命令需要set選項(xiàng)monitor處于開啟狀態(tài)時(shí)才能執(zhí)行;查看作業(yè)控制狀態(tài):輸入set -o查看monitor行;執(zhí)行set -o monitor或set -m開啟該選項(xiàng)。
  3. 該命令是bash內(nèi)建命令,相關(guān)的幫助信息請(qǐng)查看help命令。

參考鏈接

  • disown的用法


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)