# 練習 9:Bash:任務控制,`jobs`,`fg`
> 原文:[Exercise 9. Bash: job control, jobs, fg](https://archive.fo/z1oWk)
> 譯者:[飛龍](https://github.com/wizardforcel)
> 協議:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
> 自豪地采用[谷歌翻譯](https://translate.google.cn/)
Linux是一個[多任務](http://en.wikipedia.org/wiki/Computer_multitasking)操作系統。這意味著有許多程序同時運行。從用戶的角度來看,這意味著你可以同時運行幾個程序,而且 bash 肯定有工具,為你控制多個任務的執行。為了能夠使用此功能,你需要學習以下命令:
+ `<CTRL> + z` - 將當前運行的程序放在后臺。
+ `jobs` - 列出所有后臺程序。
+ `fg` - 把程序帶到前臺。`fg`接受一個數字作為參數,它可以從`jobs`中獲取數,或者如果無參數調用,則將最后一個掛起的程序帶到前臺。
+ `ctrl + c` - 一次性停止執行當前運行的程序。雖然我不會在這個練習中使用它,但我必須說,這可能是非常有用的。
現在,你將學習如何使用 bash 內置的工具來控制程序的執行。
## 這樣做
```
1: less -S .profile
2: <CTRL+z>
3: less -S .bashrc
4: <CTRL+z>
5: less -S .bash_history
6: <CTRL+z>
7: jobs
8: fg
9: q
10: fg
11: q
12: fg
13: q
14: fg
15: jobs
```
## 你會看到什么
```
user1@vm1:~$ less -S .profile
# exists.
# see /usr/share/doc/bash/examples/startup-files for
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setti
# for ssh logins, install and configure the libpam-um
#umask 022
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
[1]+ Stopped less -S .profile
user1@vm1:~$ less -S .bashrc
# for examples
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# don't put duplicate lines in the history. See bash(
# don't overwrite GNU Midnight Commander's setting of
HISTCONTROL=$HISTCONTROL${HISTCONTROL+:}ignoredups
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
[2]+ Stopped less -S .bashrc
user1@vm1:~$ less -S .bash_history
echo Hello, $LOGNAME!
echo 'echo Hello, $LOGNAME!' >> .profile
cp .profile .profile.bak
tail .profile
ls -altr
history -w
ls -al
cat .profile
echo Hello, $LOGNAME!
echo 'echo Hello, $LOGNAME!' >> .profile
cp .profile .profile.bak
tail .profile
ls -altr
[3]+ Stopped less -S .bash_history
user1@vm1:~$ jobs
[1] Stopped less -S .profile
[2]- Stopped less -S .bashrc
[3]+ Stopped less -S .bash_history
user1@vm1:~$ fg
user1@vm1:~$ fg
user1@vm1:~$ fg
user1@vm1:~$ fg
-bash: fg: current: no such job
user1@vm1:~$ jobs
user1@vm1:~$
```
## 解釋
1. 打開`.profile`來查看。注意我如何使用`-S`參數,讓`less`開啟`-chop-long-lines`選項來啟動。
1. 掛起`less`。
1. 打開`.bashrc`來查看。
1. 掛起`less`。
1. 打開`.bash_history`來查看。
1. 掛起`less`。
1. 打印掛起程序的列表。
1. 切換到`less`。
1. 退出它。
1. 切換到第二個`less`。
1. 退出它。
1. 切換到第一個`less`。
1. 退出它。
1. 嘗試切換到最后一個程序。沒有任何程序,但你這樣做是為了確保確實沒有。
1. 打印掛起程序的列表。這是為了確保沒有后臺任務,通過看到`jobs`打印出空的輸出。
## 附加題
打開`man bash`,搜索 JOB CONTROL,輸入`/, JOB CONTROL, <ENTER>`,并閱讀它。
- 笨辦法學 Linux 中文版
- 練習 0:起步
- 練習 1:文本編輯器,vim
- 練習 2:文本瀏覽器,少即是多
- 練習 3:Bash:Shell、.profile、.bashrc、.bash_history
- 練習 4:Bash:處理文件,pwd,ls,cp,mv,rm,touch
- 練習 5:Bash:環境變量,env,set,export
- 練習 6:Bash:語言設置,LANG,locale,dpkg-reconfigure locales
- 練習 7:Bash:重定向,stdin,stdout,stderr,<,>,>>,|,tee,pv
- 練習 8:更多的重定向和過濾:head,tail,awk,grep,sed
- 練習 9:Bash:任務控制,jobs,fg
- 練習 10:Bash:程序退出代碼(返回狀態)
- 練習 11:總結
- 練習 12:文檔:man,info
- 練習 13:文檔:Google
- 練習 14:包管理:Debian 包管理工具aptitude
- 練習 15:系統啟動:運行級別,/etc/init.d,rcconf,update-rc.d
- 練習 16:處理進程,ps,kill
- 練習 17:任務調度:cron,at
- 練習 18:日志:/var/log,rsyslog,logger
- 練習 19:文件系統:掛載,mount,/etc/fstab
- 練習 20:文件系統:修改和創建文件系統,tune2fs,mkfs
- 練習 21:文件系統:修改根目錄,chroot
- 練習 22:文件系統:移動數據,tar,dd
- 練習 23:文件系統:權限,chown,chmod,umask
- 練習 24:接口配置,ifconfig,netstat,iproute2,ss,route
- 練習 25:網絡:配置文件,/etc/network/interfaces
- 練習 26:網絡:封包過濾配置,iptables
- 練習 27:安全 Shell,ssh,sshd,scp
- 練習 28:性能:獲取性能情況,uptime,free,top
- 練習 29:內核:內核消息,dmesg
- 練習 30:打磨、洗練、重復:總復習
- 下一步做什么
- Debian 手動安裝