重启停止的作业 bg和fg
因为该作业是默认作业(从加号可以看出),只需要使用bg命令就可以将其以后台模式重启。 注意,当作业被转入后台模式时,并不会列出其PID。
如果有多个作业,你得在bg命令后加上作业号。
$ ./test11.sh ^Z [1]+ Stopped ./test11.sh $ ./test12.sh ^Z [2]+ Stopped ./test12.sh $ $ bg 2 [2]+ ./test12.sh & $ $ jobs [1]+ Stopped ./test11.sh [2]- Running ./test12.sh & $
命令bg 2用于将第二个作业置于后台模式。注意,当使用jobs命令时,它列出了作业及其状态,即便是默认作业当前并未处于后台模式运行。
要以前台模式重启作业,可用带有作业号的fg命令。
$ fg 2 ./test12.sh This is the script's end... $
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@localhost Desktop % macname@localhost Desktop % ./test.sh Loop #1 Loop #2 Loop #3 ^Z zsh: suspended ./test.sh macname@localhost Desktop % macname@localhost Desktop % jobs -l [1] + 48715 suspended ./test.sh macname@localhost Desktop % macname@localhost Desktop % bg [1] + continued ./test.sh Loop #4 macname@localhost Desktop % Loop #5 Loop #6 Loop #7 Loop #8 Loop #9 Loop #10 Loop #11 Loop #12 Loop #13 macname@localhost Desktop % Loop #14 Loop #15 Loop #16 Loop #17 fg [1] + running ./test.sh Loop #18 Loop #19 Loop #20 Loop #21 Loop #22 ^Z zsh: suspended ./test.sh macname@localhost Desktop % bg [1] + continued ./test.sh Loop #23 macname@localhost Desktop % Loop #24 Loop #25 Loop #26 fg [1] + running ./test.sh Loop #27 Loop #28 Loop #29 ^CGoodbye... macname@localhost Desktop %