Shell编程:同时执行两个应用程序

4 linux shell

我有两个应用程序,我们称之为APP1和APP2.我希望那两个在我的机器上并行执行.没有必要,他们在同一时间开始,但应该大致在同一时间开始.一个初步的想法是有一个shell脚本,如下所示:

./APP1&
./APP2

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

谢谢

Sta*_*ves 5

这可能更好:

./app1 & ; ./app2 & 
Run Code Online (Sandbox Code Playgroud)

但是,正如已经指出的那样,shell将作为子shell中的子进程启动它们中的每一个.shell不保证进程之间的任何同步,也不保证启动时间.

为什么需要这些并行运行?也许理解这个要求会给你一个更好的答案.

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

#!/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
Run Code Online (Sandbox Code Playgroud)


cha*_*aos 3

那会工作得很好。