zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    怎么使用 Jupyter Notebook 进入指定的目录
    安卓手机安装Xposed框架/钉钉虚拟定位
    Some of the Example google dorks 2019谷歌hacker语法检索表
    在心脏内部,普通的WEB用户会怎样? 心脏滴血漏洞分析学习
    Windows的安全配置基础,打造一个安全点的Windows
    计算机存储单位
    Python如何安装whl文件,python安装py包
    jdk文件中javaw的作用
    msfconsole基础使用,简洁高效学习版
    VirtualBox报错:不能为虚拟电脑XXX打开一个新任务
  • 原文地址:https://www.cnblogs.com/chuyiwang/p/9674347.html
Copyright © 2011-2022 走看看