Shell编程:同时执行两个应用程序 [英] Shell programming: Executing two applications at the same time

查看:96
本文介绍了Shell编程:同时执行两个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个应用程序,我们称它们为APP1和APP2.我想要那些两个在我的机器上并行执行.没必要,它们恰好从同一时间,但应该在大致相同的时间开始.最初的想法是要有一个贝壳脚本如下所示:

I have two applications, lets call them APP1 and APP2. I would like that those two execute on parallel on my machine. It is not necessary, that they start at EXACTLY the same time but the should start at roughly the same time. An intial thought was to have a shell script that looks as follows:

./APP1&
./APP2

./APP1 &
./APP2

这是技巧吗,还是我需要插入一条等待语句以确保APP2在特定时间范围内启动?

Does this the trick or do I need to insert a wait statement to make sure that APP2 starts within a certain time frame?

谢谢

推荐答案

这可能更好:

./app1 & ; ./app2 & 

但是,正如已经指出的那样,shell将在子shell中将每个子进程作为子进程启动.Shell无法保证进程之间的任何同步或启动时间.

But, as it has been pointed out, shell will start each of these as child processes in a sub-shell. No guarantees are made by the shell about any synchronization between the processes, or about the startup time.

为什么需要它们并行运行?也许理解这个要求会让你得到更好的答案.

Why do you need these to run in parallel? Perhaps understanding that requirement will get you a better answer.

您可以在这两个程序中建立一些非常简单的启动同步.这是示例的"app1"部分.

You could build some very simple startup synchronization into the two programs. Here is the "app1" part of the example.

#!/bin/sh
# app1.sh
# Do any setup, open log files, check for resources, etc, etc...

# Sync with the other app
typeset -i timeout=120 count=0
touch /tmp/app1
while [[ ! -e /tmp/app2 ]] ; do
    if [[ $count -ge $timeout ]] ; then
        print -u2 "ERROR:  Timeout waiting for app2"
        exit 1
    fi
    (( count += 1 ))
    sleep 1 
done

# Do stuff here...

# Clean up
rm /tmp/app1
exit 0

这篇关于Shell编程:同时执行两个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆