標籤: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指令碼後台運行操作