1. 程式人生 > 實用技巧 >shell nohup 讓指令碼一直以後臺模式執行到結束

shell nohup 讓指令碼一直以後臺模式執行到結束

會話中啟動shell指令碼,然後讓指令碼一直以後臺模式執行到結束,即使你退 出了終端會話。這可以用nohup命令來實現。

code

macname@MacdeMacBook-Pro Desktop % cat test.sh 
#!/bin/bash
# Trapping the script exit #
trap "echo Goodbye..." EXIT #
count=1
while [ $count -le 5 ]
do
    echo "Loop #$count"
    sleep 1
    count=$[ $count + 1 ]
done%                                                                                                                                                                                           
macname@MacdeMacBook
-Pro Desktop % macname@MacdeMacBook-Pro Desktop % macname@MacdeMacBook-Pro Desktop % nohup ./test.sh & [1] 43365 macname@MacdeMacBook-Pro Desktop % appending output to nohup.out [1] + done nohup ./test.sh macname@MacdeMacBook-Pro Desktop % cat nohup.out Loop #1 Loop #2 Loop #3 Loop #4 Loop #
5 Goodbye... macname@MacdeMacBook-Pro Desktop %