How do you run multiple programs in parallel from a bash script?

前端 未结 15 1510
眼角桃花
眼角桃花 2020-11-22 06:31

I am trying to write a .sh file that runs many programs simultaneously

I tried this

prog1 
prog2

15条回答
  •  忘掉有多难
    2020-11-22 07:03

    With bashj ( https://sourceforge.net/projects/bashj/ ) , you should be able to run not only multiple processes (the way others suggested) but also multiple Threads in one JVM controlled from your script. But of course this requires a java JDK. Threads consume less resource than processes.

    Here is a working code:

    #!/usr/bin/bashj
    
    #!java
    
    public static int cnt=0;
    
    private static void loop() {u.p("java says cnt= "+(cnt++));u.sleep(1.0);}
    
    public static void startThread()
    {(new Thread(() ->  {while (true) {loop();}})).start();}
    
    #!bashj
    
    j.startThread()
    
    while [ j.cnt -lt 4 ]
    do
      echo "bash views cnt=" j.cnt
      sleep 0.5
    done
    

提交回复
热议问题