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. Java Something

    Java静态代码检查工具 FindBugs FindBugs is a defect detection tool for Java that uses static analysis to look ...

  2. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)5.2——使用Android Testing Support Library进行测试

    问题: 你想要测试app的Android组件. 解决方案: 使用新的测试类实现JUnit风格的测试. 讨论: 测试像activities,services等的Android组件,需要将app部署到连接 ...

  3. 【BZOJ2962】序列操作 线段树

    [BZOJ2962]序列操作 Description 有一个长度为n的序列,有三个操作1.I a b c表示将[a,b]这一段区间的元素集体增加c,2.R a b表示将[a,b]区间内所有元素变成相反 ...

  4. 解决phantomjs输出中文乱码

    解决phantomjs输出中文乱码,可以在js文件里添加如下语句: phantom.outputEncoding="gb2312"; // 解决输出乱码

  5. bash短路径显示

    修改.bashrc文件vim 打开.bashrc文件,找到如下这行,有两个,都修改一下: PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' 将上面 ...

  6. git --fast-version-control

    --distributed-is-the-new-centralized 读二进制文件 python  读doc  xls 几乎所有的版本控制系统都以某种形式支持分支.使用分支意味着你可以把你的工作从 ...

  7. ArcGIS api for silverlight 禁用默认浏览操作

    ArcGIS api for silverlight 的mapcontrol中提供了一系列的默认浏览工具选项(default navigation options),如下表所示, 那么如何禁用这些默认 ...

  8. 面向对象 - 1.软件开发/2.异常处理/3.try...except的详细用法

    1.软件开发 软件的开发其实一整套规范,我们所学的只是其中的一小部分,一个完整的开发过程,需要明确每个阶段的任务,在保证一个阶段正确的前提下再进行下一个阶段的工作,称之为软件工程 面向对象的软件工程包 ...

  9. centos6.5关闭防火墙命令

    1.永久性生效,重启后不会复原 开启: chkconfig iptables on 关闭: chkconfig iptables off 2.即时生效,重启后复原 开启: service iptabl ...

  10. Redis几个认识误区(转)

    add by zhj: 文章很老了,2010年的,注意,下面几点是作者认为的误区 原文:http://timyang.net/data/redis-misunderstanding/ 前几天微博发生了 ...