这篇文章主要讲解了“Linux前后台任务实例分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Linux前后台任务实例分析”吧!
对于任务的管理,我们一般有如下几个需求:
将进程切换到前台
将进程切换到后台
查看后台任务
终止后台任务
为了演示这几个需求,我们搬出伟大的 Hello World 程序:
root@jaking-virtual-machine:~# ls -l#这三个脚本源码相同total 12 -rwxr-xr-x 1 root root 70 Feb 21 17:25 HelloWorld1.sh -rwxr-xr-x 1 root root 70 Feb 21 17:25 HelloWorld2.sh -rwxr-xr-x 1 root root 70 Feb 21 17:26 HelloWorld3.sh root@jaking-virtual-machine:~# cat HelloWorld1.sh#!/bin/bashwhile truedo echo "Hello World!" sleep 1done
开启后台任务
root@jaking-virtual-machine:~# ./HelloWorld1.sh > test1.txt &[1] 65139 root@jaking-virtual-machine:~# ./HelloWorld2.sh > test1.txt &[2] 65145 root@jaking-virtual-machine:~# ./HelloWorld3.sh > test1.txt &[3] 65155
jobs -l 查看后台任务
root@jaking-virtual-machine:~# jobs -l[1] 65139 Running ./HelloWorld1.sh > test1.txt & [2]- 65145 Running ./HelloWorld2.sh > test1.txt & [3]+ 65155 Running ./HelloWorld3.sh > test1.txt &
fg 把指定的后台任务调到前台
root@jaking-virtual-machine:~# fg %2./HelloWorld2.sh > test1.txt ^Z#Ctrl + Z将前台任务切到后台并停止运行[2]+ Stopped ./HelloWorld2.sh > test1.txt root@jaking-virtual-machine:~# jobs -l[1] 65139 Running ./HelloWorld1.sh > test1.txt & [2]+ 65145 Stopped ./HelloWorld2.sh > test1.txt [3]- 65155 Running ./HelloWorld3.sh > test1.txt &
bg 使后台停止运行的任务重新运行
root@jaking-virtual-machine:~# bg %2[2]+ ./HelloWorld2.sh > test1.txt & root@jaking-virtual-machine:~# jobs -l[1] 65139 Running ./HelloWorld1.sh > test1.txt & [2]- 65145 Running ./HelloWorld2.sh > test1.txt & [3]+ 65155 Running ./HelloWorld3.sh > test1.txt &
kill 杀掉后台进程
root@jaking-virtual-machine:~# kill 65145root@jaking-virtual-machine:~# jobs -l[1] 65139 Running ./HelloWorld1.sh > test1.txt & [2]- 65145 Terminated ./HelloWorld2.sh > test1.txt [3]+ 65155 Running ./HelloWorld3.sh > test1.txt & root@jaking-virtual-machine:~# kill %3root@jaking-virtual-machine:~# jobs -l[1]- 65139 Running ./HelloWorld1.sh > test1.txt & [2]- 65145 Terminated ./HelloWorld2.sh > test1.txt [3]+ 65155 Terminated ./HelloWorld3.sh > test1.txt
感谢各位的阅读,以上就是“Linux前后台任务实例分析”的内容了,经过本文的学习后,相信大家对Linux前后台任务实例分析这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。