shell脚本再执行过程中就一个进程,从头到尾

下面配置shell脚本执行过程中启动多个进程同时执行

#!/bin/bash

for ((i=1;i<=10;i++))
do
(
echo "$i"
sleep 10
) &
done
wait
echo -E "########## $SECONDS ##########"

注:

$SECONDS:是执行完脚本所用的时间

wait:是等待所有的进程执行完毕

执行结果

[root@wcy ~]# bash test.sh
1
2
3
4
5
6
7
8
9
10
########## 10 ##########

进程查看

[root@wcy ~]# ps -ef | grep test.sh
root 1764 1565 0 19:23 pts/1 00:00:00 bash test.sh
root 1765 1764 0 19:23 pts/1 00:00:00 bash test.sh
root 1766 1764 0 19:23 pts/1 00:00:00 bash test.sh
root 1767 1764 0 19:23 pts/1 00:00:00 bash test.sh
root 1770 1764 0 19:23 pts/1 00:00:00 bash test.sh
root 1772 1764 0 19:23 pts/1 00:00:00 bash test.sh
root 1773 1764 0 19:23 pts/1 00:00:00 bash test.sh
root 1774 1764 0 19:23 pts/1 00:00:00 bash test.sh
root 1776 1764 0 19:23 pts/1 00:00:00 bash test.sh
root 1777 1764 0 19:23 pts/1 00:00:00 bash test.sh
root 1778 1764 0 19:23 pts/1 00:00:00 bash test.sh
root 1786 1708 0 19:23 pts/2 00:00:00 grep test.sh
[root@wcy ~]# ps -ef | grep test.sh | grep -v grep | wc -l
11

查看同一时刻多少个sleep在跑

[root@wcy ~]# ps -ef | grep sleep | grep -v grep
root 2168 2165 0 21:59 pts/1 00:00:00 sleep 10
root 2169 2166 0 21:59 pts/1 00:00:00 sleep 10
root 2172 2167 0 21:59 pts/1 00:00:00 sleep 10
root 2174 2171 0 21:59 pts/1 00:00:00 sleep 10
root 2175 2173 0 21:59 pts/1 00:00:00 sleep 10
root 2176 2170 0 21:59 pts/1 00:00:00 sleep 10
root 2179 2177 0 21:59 pts/1 00:00:00 sleep 10
root 2181 2178 0 21:59 pts/1 00:00:00 sleep 10
root 2182 2180 0 21:59 pts/1 00:00:00 sleep 10
root 2184 2183 0 21:59 pts/1 00:00:00 sleep 10

  

[root@wcy ~]# ps -ef | grep sleep | grep -v grep  | wc -l
10

多进程的shell脚本可以用于并行执行多任务

#!/bin/bash
for ((i=1;i<=1;i++))
do
(
for ((num=1;num<=100;num++))
do
echo "task1-- $num"
sleep 1
done
) &
(
for ((ber=1;ber<=100;ber++))
do
echo "task2-- $ber"
sleep 1
done
) &
done
wait

效果,两个同时执行

[root@wcy ~]# bash duo.sh
task2-- 1
task1-- 1
task2-- 2
task1-- 2
task2-- 3
task1-- 3
task2-- 4
task1-- 4
········

脚本进程

[root@wcy ~]# ps -ef | grep duo.sh | grep -v grep
root 2221 1491 0 22:13 pts/0 00:00:00 bash duo.sh
root 2222 2221 0 22:13 pts/0 00:00:00 bash duo.sh
root 2223 2221 0 22:13 pts/0 00:00:00 bash duo.sh

同时执行的进程

[root@wcy ~]# ps -ef | grep sleep | grep -v grep
root 2357 2223 0 22:14 pts/0 00:00:00 sleep 1
root 2358 2222 0 22:14 pts/0 00:00:00 sleep 1

shell脚本多进程的更多相关文章

  1. curl命令,curl实现post,curl监控网页shell脚本,curl多进程实现并控制进程数,

    cURL > Docs > Tutorial:  http://curl.haxx.se/docs/httpscripting.html 下载单个文件,默认将输出打印到标准输出中(STDO ...

  2. shell脚本实现轮询查看进程是否结束

    功能需求: 一个shell脚本,为了使用多进程,启动十几个后台运行的程序,为了防止脚本比后台进程提前结束造成不可预估的影响,现要判断是否多个后台执行的已知进程已经结束,并在所有进程结束后做出相应操作. ...

  3. shell脚本实现进度条

    使用shell脚本编写进度条 可已加入到shell脚本当中 主要作用:好看 美观 没毛用 (一) 普通进度条: #!/bin/bashb='' for ((i=0;$i<=20;i++)) do ...

  4. 浅谈自底向上的Shell脚本编程及效率优化

    作者:沐星晨 出处:http://blog.csdn.net/sosodream/article/details/6276758 浅谈自底向上的Shell脚本编程及效率优化 小论文,大家多批评指导:) ...

  5. shell 脚本定制与重定向

    脚本定制 . 或者 source: 读取文本文件并执行(在当前shell解释并执行) source ./ld 总用量 8 -rw-------. 1 root root 1223 10月 2 21:1 ...

  6. 用 shell 脚本做自动化测试

    前言 项目中有一个功能,需要监控本地文件系统的变更,例如文件的增.删.改名.文件数据变动等等.之前只在 windows 上有实现,采用的是 iocp + ReadDirectoryChanges 方案 ...

  7. 100个Linux Shell脚本经典案例(附PDF)

    转载自:https://mp.weixin.qq.com/s/tCKAM67_7K7q2vJthaIsDQ 原文链接:https://wenku.baidu.com/view/4f089430a116 ...

  8. 第一个shell脚本

    打开文本编辑器,新建一个文件,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好. #!/bin/bash echo "Hello World !" &quo ...

  9. 使用C#给Linux写Shell脚本

    在这个逼格决定人格,鄙视链盛行的年头,尤其是咱们IT界,请问您今天鄙视与被鄙视的次数分别是多少?如果手中没有一点压箱的本事,那就只有看的份了.今天我们也要提升下自己的格调,学习些脑洞大开的东西,学完之 ...

随机推荐

  1. util.inherits

    util.inherits util.inherits(constructor, superConstructor)是一个实现对象间原型继承 的函数. JavaScript 的面向对象特性是基于原型的 ...

  2. 模拟ORA-26040: Data block was loaded using the NOLOGGING option

    我们知道通过设置nologging选项.能够加快oracle的某些操作的运行速度,这在运行某些维护任务时是非常实用的,可是该选项也非常危急,假设使用不当,就可能导致数据库发生ORA-26040错误. ...

  3. Trifo-VIO:Roubst and Efficient Stero Visual Inertial Odometry using Points and Lines论文笔记

    这是2018-IROS上的一篇文章,亮点是作者提出了Lines特征的VIO方案,还有就是提出一个新颖的回环检测,不是用传统的基于优化的方法或者BA,另外作者还发布了一个新的用于VIO的数据集.亮点主要 ...

  4. ORACLE的测试用户Scott

    Oracle数据库的测试用户Scott的密码为什么是Tiger? 1977年6月,Larry Ellison 与 Bob Miner 和 Ed Oates 在硅谷共同创办了一家名为软件开发实验室(So ...

  5. 淘宝订单数据转CSV

    <html> <body> <div id="result"> </div> <div> <textarea st ...

  6. 七、H5 直播视频播放

    HTML5实现视频直播功能思路详解_html5教程技巧_脚本之家 https://m.jb51.net/html5/587215.html 七.H5 直播视频播放 移动端iOS和 Android 都天 ...

  7. Spring 框架的AOP之注解的方式

    1. 环境搭建 1.1 导入 jar 包 Spring 框架的基本开发包(6个); Spring 的传统AOP的开发包 spring-aop-4.3.10.RELEASE org.aopallianc ...

  8. Java 语言基础之语句

    程序的四种流程控制结构: 顺序结构 判断结构 : if 语句 选择结构 : switch 语句 循环结构 : while 语句, do...while 语句, for 语句 以下主要分析循环结构: w ...

  9. 洛谷P4475 巧克力王国

    洛谷P4475 巧克力王国 题目描述 巧克力王国里的巧克力都是由牛奶和可可做成的. 但是并不是每一块巧克力都受王国人民的欢迎,因为大家都不喜欢过于甜的巧克力. 对于每一块巧克力,我们设 x 和 y 为 ...

  10. 我的Android进阶之旅------>Android自定义View来实现解析lrc歌词并同步滚动、上下拖动、缩放歌词的功能

    前言 一LRC歌词文件简介 1什么是LRC歌词文件 2LRC歌词文件的格式 LRC歌词文件的标签类型 1标识标签 2时间标签 二解析LRC歌词 1读取出歌词文件 2解析得到的歌词内容 1表示每行歌词内 ...