Linux 任務控制

來源:互聯網
上載者:User

標籤:style   blog   color   使用   sp   檔案   div   on   log   

Linux/Unix 提供了不同與 windows 的多任務處理,也提供一套用來切換前背景工作的命令

bg fg & Ctrl+z nohup sitsid

  1. Ctrl-Z  掛起程式
    [email protected]:~$ ./test.sh^Z[1]+  Stopped                 ./test.sh[email protected]:~$

    當作業在運行期間,執行 Ctrl-Z 暫掛起該作業並返回該作業的 JobID 等資訊,類似於使用 jobs 命令返回的資訊

  2. bg  後台執行
    [email protected]:~$ jobs[1]+  Stopped                 ./test.sh、[email protected]:~$ bg 1[1]+ ./test.sh &[email protected]:~$

    bg 將暫掛的作業作為後台作業運行來在當前環境中重新執行這些作業,如果指定的作業已經在後台運行,bg 命令不起作用並成功退出。如果未提供 JobID 參數,bg 命令會使用最近暫掛的作業

  3. fg  前台運行作業
    [email protected]:~$ jobs[1]+  Stopped                 ./test.sh、[email protected]:~$ fg 1./test.sh[email protected]:~$

    fg 使用 JobID 參數來指明在前台下要啟動並執行特定作業。如果提供 JobID,fg 命令使用最近在後台被暫掛的作業

  4. &  把作業放到後台執行
    [email protected]:~$ ./test.sh &[1] 33511[email protected]:~$

    當在前台運行某個作業時,終端被該作業佔據;可以使用&命令把作業放到後台執行,而在後台運行作業時,它不會佔據終端。實際上,這樣是將命令放入到一個作業隊列中了。作業在後台運行一樣會將結果輸出到螢幕上,幹擾你的工作。如果放在後台啟動並執行作業會產生大量的輸出,最好使用下面的方法把它的輸出重新導向到某個檔案中:command >out.file 2>&1 &

  5. nohup  忽略掛斷訊號
    [email protected]:~$ nohup ./test.sh > test.log 2>&1 &[1] 33537[email protected]:~$ jobs

    在執行作業時,可以使用小括弧() 執行後台作業也能做到類似 nuhup 的效果,使得 當前 Shell 關閉後,作業仍能正常運行。原理同 setsid 和 disown

    [email protected]:~$ (./test.sh &)[email protected]:~$ ps -ef | grep test515        410     1 0 11:49 ?        00:00:00 /bin/sh ./test.sh515      12483 21734 0 11:59 pts/12   00:00:00 grep test

    使用 & | bg 運行後台命令時,其父進程還是當前終端 shell 的進程,而一旦父進程退出,則會發送hangup訊號給所有子進程,子進程收到hangup以後也會退出。如果我們要在退出shell的時候繼續運行進程,則需要使用nohup忽略hangup訊號,或者使用setsid將父進程設為init進程(進程號為1),也可以使用 disown 命令

    1. setsid
      [email protected]:~$ ps -ef | grep test515      29710 21734 0 11:47 pts/12   00:00:00 /bin/sh ./test.sh515      29713 21734 0 11:47 pts/12   00:00:00 grep test[email protected]:~$ setsid ./test.sh &[1] 409[email protected]:~$ ps -ef | grep test515        410     1 0 11:49 ?        00:00:00 /bin/sh ./test.sh515        413 21734 0 11:49 pts/12   00:00:00 grep test

      對於已經執行的前台作業,setsid 可以將當前作業的父進程設定為1,但對於已經是後台啟動並執行命令我們需要使用 disown 命令

    2. disown
      [email protected]:~$ ./test.sh &[1] 2539[email protected]:~$ jobs -l[1]+ 2539 Running                 ./test.sh &[email protected]:~$ disown -h %1[email protected]:~$ ps -ef | grep test515        410     1 0 11:49 ?        00:00:00 /bin/sh ./test.sh515       2542 21734 0 11:52 pts/12   00:00:00 grep test

      對於已經執行的後台作業,disown 可以將當前作業的父進程設定為1

Linux 任務控制

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.