Managing linux Shell Jobs

Managing Shell Jobs  

When moving jobs between the foreground and background, it may be useful to have an overview of all current jobs. To get such an overview, use the  jobs command. As you can see in  Table   9.2   , this command gives an overview of all jobs currently running as a background job, including the job number assigned to the job when starting it in the background. These job numbers can be used as an argument to the  fg  and  bg  commands to perform job management tasks. In  Exercise 9.1 , you learn how to perform common job management tasks from the shell.

一个很好的练习Jobs管理的实例:

Exercise 9.1 Managing jobs In this exercise, you apply the commands that you just learned about to manage jobs that have been started from the current shell.

1. Open a root shell and type the following commands:

sleep 3600 &dd if=/dev/zero of=/dev/null &sleep 7200

2. Because you started the last command with no & after the command, you have to wait 2 hours before you get control to the shell back. Type Ctrl+Z to stop it.

3. Type jobs . You will see the three jobs that you just started. The first two of them have the Running state, and the last job currently is in the Stopped state.

[[email protected] ~]# sleep 3600 &
[1] 2464
[[email protected] ~]# dd if=/dev/zero of=/dev/null &
[2] 2465

 [[email protected] ~]# sleep 7200
 ^Z
 [3]+ Stopped sleep 7200
 [[email protected] ~]# jobs
  [1] Running sleep 3600 &
  [2]- Running dd if=/dev/zero of=/dev/null &
  [3]+ Stopped sleep 7200

4. Type bg 3 to continue running job 3 in the background. Notice that because it was started as the last job, you did not really have to add the number 3.

[[email protected] ~]# bg 3
[3]+ sleep 7200 &
[[email protected] ~]# jobs
[1]   Running                 sleep 3600 &
[2]-  Running                 dd if=/dev/zero of=/dev/null &
[3]+  Running                 sleep 7200 &
[[email protected] ~]# 

5. Type fg 1 to move job 1 to the foreground.

6. Type Ctrl+C to cancel job number 1 and use jobs to confirm that it is now gone.

7. Use the same approach to cancel jobs 2 and 3 also.

[[email protected] ~]# fg 1
sleep 3600
^C
[[email protected] ~]# jobs
[2]-  Running                 dd if=/dev/zero of=/dev/null &
[3]+  Running                 sleep 7200 &
[[email protected] ~]# fg 2
dd if=/dev/zero of=/dev/null
^C590233206+0 records in
590233205+0 records out
302199400960 bytes (302 GB) copied, 372.251 s, 812 MB/s

[[email protected] ~]# jobs
[3]+  Running                 sleep 7200 &
[[email protected] ~]# fg 3
sleep 7200
^C
[[email protected] ~]# jobs
[[email protected] ~]# 

8. Open a second terminal on your server.

9. From that second terminal, type dd if=/dev/zero of=/dev/null & .

10. Type exit to close the second terminal.

11. From the other terminal, start top . You will see that the dd job is still running. From top, use k to kill the dd job.

时间: 2024-10-10 17:11:08

Managing linux Shell Jobs的相关文章

Monitoring and Managing Linux Processes

Overview Goal To evaluate and control processes running on a Lunix system Objectives List and interpret basic information about processes running on the system Control processes in the shell? session using bash job control Terminate and control porce

linux shell中的比较符号与特殊符号介绍

shell字符串比较.判断是否为数字  二元比较操作符,比较变量或者比较数字.注意数字与字符串的区别.  整数比较  -eq 等于,如:if [ "$a" -eq "$b" ]  -ne  不等于,如:if [ "$a" -ne "$b" ]  -gt 大于,如:if [ "$a" -gt "$b" ]  -ge 大于等于,如:if [  "$a" -ge "

Linux Shell下的后台运行及其前台的转换

当用bash启动一个程序,你可以用在程序后加&,实现程序在后台运行: 例如: emacs & 但如果你忘了输入&;但你又不想把程序停止后重新启动: 你可以这么做: 按Ctrl + Z暂停前台工作,比如前面提到的Emacs: 然后用jobs命令查看当前bash一共启动了多少程序,你可以看到你的emacs在其中,它的状态是Stoped的; 并且它的jobID也可以看,比如是2 然后用命令bg 2 这样你就可以达到和emacs &一样的效果了. 当你有些工作比如updatedb,

Linux Shell脚本编程while语句案例

1,每隔3秒,打印一次系统负载 #!/bin/bash while true do uptime sleep 3 done 2,把监控结果保存到文件,在后台执行,然后用tail -f监控文件变化 [email protected]:~/linux/shell/flow_control$ sh while.sh & [1] 12867 #!/bin/bash while true do uptime >> log.txt sleep 3 done [email protected]:~/

Linux Shell脚本编程while语句

Linux Shell脚本编程while语句案例 1,每隔3秒,打印一次系统负载 #!/bin/bash while truedo    uptime    sleep 3done 2,把监控结果保存到文件,在后台执行,然后用tail -f监控文件变化[email protected]:~/linux/shell/flow_control$ sh while.sh &[1] 12867 #!/bin/bash while truedo    uptime >> log.txt    s

Linux Shell的父子关系及内建命令

Linux Shell的父子关系及内建命令 Shell的类型 系统启动什么样的shell程序取决于你个人的用户ID配置.在/etc/passwd文件中,在用户ID记录的第7个字段中列出了默认的shell程序.只要用户登录到某个虚拟控制台终端或是在GUI中启动终端仿真器,默认的shell程序就会开始运行. 在下面的例子中,用户xiaoyu使用了GNU bash shell作为自己的默认shell程序: 1 [email protected]:/$ cat /etc/passwd 2 [...] 3

Linux Shell sort 指定排序第几列

ip.txt 里存储着ip信息 统计排序后取前10条 awk '{cnt[$1]++} END{for (ip in cnt) print ip":"cnt[ip]}' ip.txt | sort -k 2 -rn -t":" | head -n 10 awk '{cnt[$1]++} END{for (ip in cnt) print cnt[ip],ip}' ip.txt | sort -rn | head -n 10 sort -k  根据第几列排序  -n

linux shell脚本执行错误:bad substitution

脚本test.sh内容: #!/bin/bash read pressKey indexes=0 c=${pressKey:indexes:1} 使用调试方式执行:sh -x test.sh第3行总出现bad substitution提示信息. 百思不得其解: 于是百度,查到一条有用信息,这与linux shell使用的是/bin/sh,还是/bin/bash有关系.我的脚本中指定使用的是/bin/bash shell,但是我在调试的时候使用的是sh shell,因此调试时导致错误提示信息. 解

linux shell 数组建立及使用技巧

转自linux shell 数组建立及使用技巧 linux shell在编程方面比windows 批处理强大太多,无论是在循环.运算.已经数据类型方面都是不能比较的. 下面是个人在使用时候,对它在数组方面一些操作进行的总结. 1.数组定义 [[email protected] ~]$ a=(1 2 3 4 5)[[email protected] ~]$ echo $a1 一对括号表示是数组,数组元素用“空格”符号分割开. 2.数组读取与赋值 得到长度: [[email protected] ~