|
2019-07-28
- [root@server3 ~]# ps -o command,state
- COMMAND S
- -bash S
- ps -o command,state R
复制代码
- [root@server3 ~]# ps -o command,state
- COMMAND S
- -bash S
- ps -o command,state R
- [root@server3 ~]# sleep 600
- ^Z
- [1]+ Stopped sleep 600
- [root@server3 ~]# ps -o command,state
- COMMAND S
- -bash S
- sleep 600 T
- ps -o command,state R
- [root@server3 ~]#
复制代码 stop和sleep的区别就是,ps能看见的是sleep(sleep是在等待资源的一种运行)而stop看不见:
- [root@server3 ~]# ps
- PID TTY TIME CMD
- 18587 pts/0 00:00:00 bash
- 18910 pts/0 00:00:00 sleep
- 18928 pts/0 00:00:00 ps
- [root@server3 ~]#
复制代码 恢复运行,而后台的运行就叫S:
- [root@server3 ~]# ps -o command,state
- COMMAND S
- -bash S
- sleep 600 T
- ps -o command,state R
- [root@server3 ~]# bg
- [1]+ sleep 600 &
- [root@server3 ~]# ps -o command,state
- COMMAND S
- -bash S
- sleep 600 S
- ps -o command,state R
- [root@server3 ~]# jobs
- [1]+ Running sleep 600 &
复制代码
执行作业控制:A命令(sleep 10)和B命令(sleep 13)要高效同时执行,A、B两个命令运行结束时要分别马上通知用户:
1. 低效的程序设计:
- #!/bin/sh
- sleep 10
- echo "finished 10"
- sleep 13
- echo "finished 13"
复制代码 一共要执行23秒
2. 高效的程序设计,但是A没执行完成就通知用户:
- #!/bin/sh
- sleep 10 &
- echo "finished 10"
- sleep 13
- echo "finished 13"
复制代码 一共要执行13秒
3. 高效的程序设计,但是A执行完成没有及时通知用户- #!/bin/sh
- sleep 10 &
- sleep 13 &
复制代码 4. 正确的写法:- #!/bin/sh
- (sleep 10 ; echo "finished 10" )&
- sleep 13
- echo "finished 13"
复制代码
----------------------------------------------------------------------------------------------
- [root@desktop3 ~]# ps
- PID TTY TIME CMD
- 9935 pts/1 00:00:00 bash
- 10176 pts/1 00:00:00 ps
- [root@desktop3 ~]# sleep 111
- ^Z
- [1]+ Stopped sleep 111
- [root@desktop3 ~]# ps j
- PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
- 7745 8048 8048 8048 tty1 8048 Ssl+ 0 0:03 /usr/bin/X :0 -bac
- 9926 9935 9935 9935 pts/1 10185 Ss 0 0:00 -bash
- 9935 10183 10183 9935 pts/1 10185 T 0 0:00 sleep 111
- 9935 10185 10185 9935 pts/1 10185 R+ 0 0:00 ps j
- [root@desktop3 ~]# sleep 222 &
- [2] 10186
- [root@desktop3 ~]# ps j
- PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
- 7745 8048 8048 8048 tty1 8048 Ssl+ 0 0:03 /usr/bin/X :0 -bac
- 9926 9935 9935 9935 pts/1 10187 Ss 0 0:00 -bash
- 9935 10183 10183 9935 pts/1 10187 T 0 0:00 sleep 111
- 9935 10186 10186 9935 pts/1 10187 S 0 0:00 sleep 222
- 9935 10187 10187 9935 pts/1 10187 R+ 0 0:00 ps j
- [root@desktop3 ~]# bg %1
- [1]+ sleep 111 &
- [root@desktop3 ~]# ps j
- PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
- 7745 8048 8048 8048 tty1 8048 Ssl+ 0 0:03 /usr/bin/X :0 -bac
- 9926 9935 9935 9935 pts/1 10198 Ss 0 0:00 -bash
- 9935 10183 10183 9935 pts/1 10198 S 0 0:00 sleep 111
- 9935 10186 10186 9935 pts/1 10198 S 0 0:00 sleep 222
- 9935 10198 10198 9935 pts/1 10198 R+ 0 0:00 ps j
- [root@desktop3 ~]#
复制代码 -----------------------------------------------------------------------
|
|