Linux中jobs的管理常用命令
Linux中jobs的管理常用命令
1 将命令或者脚本放到后台去运行
#命令 &
eg
rpm -ivh zlib**.rpm &
./run.sh &
2 将命令暂停到后台
Ctrl+z
3 查看后台命令
#jobs
参数:
-l: 除了列出作业号之外同时列出PID
-r: 列出仅在后台运行(run)的作业
-s:仅列出暂停的作业
4 把暂停作业拿到后台处理 bg
Ctrl+z 先把作业暂停到后台
#bg job_id 实现把作业调到后台执行
5 把后台作业调到前台处理 fg
#fg job_id
6 用kill发送信号来操作jobs
kill -signal %job_id 这里必须要加%,否则会有惊喜,嘿嘿
参数:
-l 列出当前kill能够使用的信号
signal:表示给后台的作业什么指示,用man 7 signal可知
-1(数字):重新读取一次参数的设置文件(类似reload)
-2:表示与由键盘输入ctrl-c同样的动作
-9:立刻强制删除一个作业
-15:以正常方式终止一项作业。与-9不一样。
演示:
amp1:~ # cat jobs.sh ##jobs脚本,每两秒钟输入时间到jobs.txt中 #!/bin/bash while : do sleep 2 time=`date +"%H-%M-%S"` echo "this time is $time" >> jobs.txt done amp1:~ # ./jobs.sh & ##放到后台运行 [1] 50770 You have new mail in /var/mail/root amp1:~ # amp1:~ # jobs ##查看作业 [1]+ Running ./jobs.sh & amp1:~ # fg 1 ##调用到前台运行 ./jobs.sh ^Z ##ctrl+z 调用暂停 [1]+ Stopped ./jobs.sh amp1:~ # watch cat jobs.txt ##动态显示jobs.txt写入的状态 this time is 20-10-49 this time is 20-10-51 this time is 20-10-53 this time is 20-10-55 this time is 20-10-57 this time is 20-10-59 this time is 20-11-01 this time is 20-11-03 this time is 20-11-05 this time is 20-11-07 this time is 20-11-09 this time is 20-11-11 this time is 20-11-13 this time is 20-11-15 ##看这个时间,现在是暂停的状态 ------------------------------------------------------------------ amp1:~ # bg 1 ##调用暂停作业,到后台运行 [1]+ ./jobs.sh & You have new mail in /var/mail/root amp1:~ # jobs ##jobs运行状态 [1]+ Running ./jobs.sh & amp1:~ # 接着上面的amp1:~ # watch cat jobs.txt this time is 20-10-49 this time is 20-10-51 this time is 20-10-53 this time is 20-10-55 this time is 20-10-57 this time is 20-10-59 this time is 20-11-01 this time is 20-11-03 this time is 20-11-05 this time is 20-11-07 this time is 20-11-09 this time is 20-11-11 this time is 20-11-13 this time is 20-11-15 this time is 20-18-00 //这里已经暂停了7分钟,然后再次启动,说明bg命令生效了 this time is 20-18-02 this time is 20-18-04 this time is 20-18-06 this time is 20-18-08 this time is 20-18-10 this time is 20-18-12 amp1:~ # kill -9 %1 #成功的杀死jobs amp1:~ # kill -9 %1 -bash: kill: %1: no such job You have new mail in /var/mail/root amp1:~ # jobs amp1:~ # jobs
本文出自 “天道酬勤” 博客,请务必保留此出处http://luzhi1024.blog.51cto.com/8845546/1651727
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。