shell指令碼後台運行操作

來源:互聯網
上載者:User

標籤:shell

最近幫同學寫一個簡單指令碼= =(別噴我~~),狀況頻出,後台運行方式和ctrl+Z概念沒有理解清楚搞出了大笑話,一下晾出指令碼和解決過程:

#!/bin/bash    while :;do          a=`date +%Y/%m%d%T`          java=`ps aux |grep vsftpd |awk ‘NR==1{print $3}‘`          echo "時間$a,CPU使用率$java" >> xiangchen/1.txt        sleep 10    done

當時直接使用Ctrl+Z以為是直接後台運行,後來瞭解到只是掛起前台任務,掛起並沒有後台運行,呵呵- -
後來百度了一下找到解決方案是把運行中的任務Ctrl+Z暫停掛起:
  #  sh vsftpd.sh &
  #  [1] 1159
  #  jobs -l
  #  [1]+  1159 Running                 ./vsftpd.sh &           //查看在後台運行
  #  $ disown -h %1
  #  $ ps -ef | grep vsftpd
  #  root      1492  1143  0 15:42 pts/0    00:00:00 /bin/bash ./vsftpd.sh
  #  root      1531  1143  0 15:43 pts/0    00:00:00 grep vsftpd
這裡我們可以讓程式末尾+&掛起,然後使用disown -h %程式序號來讓其後台運行,不受當前shell退出影響。還有一種方法,和這個效果相同:
  # (./vsftpd.sh &)
  # [1]+  Killed                  ./vsftpd.sh
  # ps -ef |grep vsftpd
  # root      1578     1  0 15:50 ?        00:00:00 /bin/bash ./vsftpd.sh
  # root      1699  1615  0 15:52 pts/2    00:00:00 grep vsftpd
 結束指令碼進程:kill -9 進程號  
[[email protected] ~]# kill -9 1578
[[email protected] ~]# ps -ef |grep vsftpd
root      1708  1615  0 15:55 pts/2    00:00:00 grep vsftpd
系統開機時運行指令碼寫入開機啟動設定檔中即可


記錄每天想到的,堅持下去吧= =

本文出自 “linux窩” 部落格,請務必保留此出處http://xiangcc.blog.51cto.com/10201823/1649634

shell指令碼後台運行操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.