shell脚本多进程
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脚本多进程的更多相关文章
- curl命令,curl实现post,curl监控网页shell脚本,curl多进程实现并控制进程数,
cURL > Docs > Tutorial: http://curl.haxx.se/docs/httpscripting.html 下载单个文件,默认将输出打印到标准输出中(STDO ...
- shell脚本实现轮询查看进程是否结束
功能需求: 一个shell脚本,为了使用多进程,启动十几个后台运行的程序,为了防止脚本比后台进程提前结束造成不可预估的影响,现要判断是否多个后台执行的已知进程已经结束,并在所有进程结束后做出相应操作. ...
- shell脚本实现进度条
使用shell脚本编写进度条 可已加入到shell脚本当中 主要作用:好看 美观 没毛用 (一) 普通进度条: #!/bin/bashb='' for ((i=0;$i<=20;i++)) do ...
- 浅谈自底向上的Shell脚本编程及效率优化
作者:沐星晨 出处:http://blog.csdn.net/sosodream/article/details/6276758 浅谈自底向上的Shell脚本编程及效率优化 小论文,大家多批评指导:) ...
- shell 脚本定制与重定向
脚本定制 . 或者 source: 读取文本文件并执行(在当前shell解释并执行) source ./ld 总用量 8 -rw-------. 1 root root 1223 10月 2 21:1 ...
- 用 shell 脚本做自动化测试
前言 项目中有一个功能,需要监控本地文件系统的变更,例如文件的增.删.改名.文件数据变动等等.之前只在 windows 上有实现,采用的是 iocp + ReadDirectoryChanges 方案 ...
- 100个Linux Shell脚本经典案例(附PDF)
转载自:https://mp.weixin.qq.com/s/tCKAM67_7K7q2vJthaIsDQ 原文链接:https://wenku.baidu.com/view/4f089430a116 ...
- 第一个shell脚本
打开文本编辑器,新建一个文件,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好. #!/bin/bash echo "Hello World !" &quo ...
- 使用C#给Linux写Shell脚本
在这个逼格决定人格,鄙视链盛行的年头,尤其是咱们IT界,请问您今天鄙视与被鄙视的次数分别是多少?如果手中没有一点压箱的本事,那就只有看的份了.今天我们也要提升下自己的格调,学习些脑洞大开的东西,学完之 ...
随机推荐
- 转载:resNet论文笔记
<Deep Residual Learning for Image Recognition>是2016年 kaiming大神CVPR的最佳论文 原文:http://m.blog.csdn. ...
- dropload 使用表
移动端下拉刷新.上拉加载更多插件 依赖 (dependence) Zepto 或者 jQuery 1.7以上版本,推荐jQuery 2.x版本(二者不要同时引用) Zepto or jQuery 1. ...
- Configuration注解类 Bean解析顺序
@PropertySource 加载properties @ComponentScan 扫描包 @Import 依赖的class @ImportResource 依赖的xml @Bean 创建bean ...
- 【BZOJ1264】[AHOI2006]基因匹配Match DP+树状数组
[BZOJ1264][AHOI2006]基因匹配Match Description 基因匹配(match) 卡卡昨天晚上做梦梦见他和可可来到了另外一个星球,这个星球上生物的DNA序列由无数种碱基排列而 ...
- 《从零开始学Swift》学习笔记(Day 61)——Core Foundation框架之内存管理
原创文章,欢迎转载.转载请注明:关东升的博客 在Swift原生数据类型.Foundation框架数据类型和Core Foundation框架数据类型之间转换过程中,虽然是大部分是可以零开销桥接,零开销 ...
- mysql中给表添加字段
添加字段: 格式:alter table 表名 add 字段名 字段类型 ; 如:给表stu_info 添加一个字段type,类型为varchar(30) alter table stu_info a ...
- java获取地址全路径
String basePath = request.getScheme()+"://"+request.getServerName()+":"+reques ...
- Struts2.0 封装请求数据和拦截器介绍
1. Struts2 框架中使用 Servlet 的 API 来操作数据 1.1 完全解耦合的方式 Struts2 框架中提供了一个 ActionContext 类,该类中提供了一些方法: stati ...
- Delphi中MD5实现方法(转)
原来写过一个计算MD5的程序,是用了一个叫MD5.pas的单元,使用起来还算简单,但还有更简单的办法,安装了indy就会有IdHashMessageDigest单元(delphi 7默认安装indy) ...
- windows 键盘全局钩子
// HookapiTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> #inc ...