bash 等待一个进程完成并执行另一个进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37205010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:37:58  来源:igfitidea点击:

wait one process to finish and execute another process

linuxbashshellubuntu

提问by eakn

I want to make a synchronization between the process.My computer has 2 core.User can enter the simulation number from command line.If input is greater than the 2, the 3rd and rest processes has to wait until one of the processes is finished.If one of them is finished, next process should be executed.For example, first 2 process is already proceeding and lets say, 1th one is finished before 2nd process.Now 3rd process should be executed.I am new in bash, I figured out.It is seen that anywait: command not found.How can I do that? Here is my script:

我想在进程之间进行同步。我的电脑有2个内核。用户可以从命令行输入模拟数。如果输入大于2,则第3个和其余进程必须等到其中一个进程完成。如果其中一个完成,则应该执行下一个过程。例如,第一个 2 个过程已经在进行中,假设第一个在第二个过程之前完成。现在应该执行第三个过程。我是 bash 新手,我想通了. 可以看到 anywait: command not found. 我该怎么做?这是我的脚本:

#!/bin/bash
# My first script

count=2
echo -n "Please enter the number of simulation :"
read number
echo "Please enter the algorithm type  "
printf "0 for NNA\n1 for SPA\n2 for EEEA :"

while read type; do
    case $type in
        0 ) cd /home/cea/Desktop/simulation/wsnfuture 
        taskset -c 0 ./wsnfuture -u Cmdenv omnetpp.ini > /home/cea/Desktop/simulation/RESULTS/NNA/NNA0/0 &
        taskset -c 1 ./wsnfuture -u Cmdenv omnetpp.ini > /home/cea/Desktop/simulation/RESULTS/NNA/NNA0/1 &
        while [ $count -lt $number ]; do
        anywait
            cd /home/cea/Desktop/simulation/wsnfuture 
        mkdir /home/cea/Desktop/simulation/RESULTS/NNA/NNA$count
        taskset -c $((count % 2)) ./wsnfuture -u Cmdenv omnetpp.ini > /home/cea/Desktop/simulation/RESULTS/NNA/NNA$count/$count &
            count=$((count + 1))
        done 
        ;;
        1 ) while [ $count -lt $number ]; do
            cd /home/cea/Desktop/simulation/wsnfuture1
        taskset -c $((count % 2)) ./wsnfuture -u Cmdenv omnetpp.ini > /home/cea/Desktop/simulation/RESULTS/SPA/$count &
            count=$((count + 1))
        done 
        ;;
        2 ) while [ $count -lt $number ]; do
            cd /home/cea/Desktop/simulation/wsnfuture2
        taskset -c $((count % 2)) ./wsnfuture -u Cmdenv omnetpp.ini > /home/cea/Desktop/simulation/RESULTS/EEEA/$count &
            count=$((count + 1))
        done 
        ;;
        * ) echo "You did not enter a number"
        echo "between 0 and 2."
        echo "Please enter the algorithm type  "
        printf "0 for NNA\n1 for SPA\n2 for EEEA :"

    esac

done

function anywait(){
 while ps axg | grep -v grep | grep wsnfuture> /dev/null; do sleep 1; done
} 

回答by Inian

You can achieve a simple way of process synchronization in bashusing waitwhich waits for one or more number of background jobs to complete before running the next.

您可以在实现过程中的简单的同步方式bash使用wait这对于后台作业的一个或多个号码等待运行下一个之前完成。

You generally run jobs in the background by appending the &operator to the end of a command. At that point the PID(process ID) of the newly created background process is stored in a special bash variable: $!and waitcommand allows this process to be terminate before running the next instruction.

您通常通过将&运算符附加到命令末尾来在后台运行作业。此时,PID新创建的后台进程的(进程 ID)存储在一个特殊的 bash 变量中:$!并且waitcommand 允许在运行下一条指令之前终止该进程。

This can be demonstrated by a simple example

这可以通过一个简单的例子来证明

$ cat mywaitscript.sh

#!/bin/bash

sleep 3 &

wait $!     # Can also be stored in a variable as pid=$!

# Waits until the process 'sleep 3' is completed. Here the wait on a single process is done by capturing its process id

echo "I am waking up"

sleep 4 &
sleep 5 &

wait                    # Without specifying the id, just 'wait' waits until all jobs started on the background is complete.

echo "I woke up again"

Command ouput

命令输出

$ time ./mywaitscript.sh
I am waking up
I woke up again

real    0m8.012s
user    0m0.004s
sys     0m0.006s

You can see the script has taken ~8s to run to completion. The breakdown on the time is

您可以看到脚本运行完成需要大约 8 秒。时间细分是

  1. sleep 3will take full 3s to complete its execution

  2. sleep 4and sleep 5are both started sequentially one after next and it has taken the max(4,5) which is approximately ~5s to run.

  1. sleep 3将需要整整 3 秒才能完成执行

  2. sleep 4并且sleep 5都一个接一个地依次启动,并且运行了大约 5 秒的 max(4,5)。

You can apply the similar logic to your question above. Hope this answers your question.

您可以将类似的逻辑应用于上述问题。希望这能回答你的问题。

回答by Camusensei

Your code has many other problems, but the answer is that you should declare anywait before using it (so moving it up in your script).

您的代码还有许多其他问题,但答案是您应该在使用它之前声明 anywait(因此将它移到您的脚本中)。

Please consider using http://www.shellcheck.net/to at least suppress the most obvious errors/mistakes in your script.

请考虑使用http://www.shellcheck.net/来至少抑制脚本中最明显的错误/错误。