如何在这个后台进程的bash脚本中编程

时间:2011-11-23 02:11:23

标签: bash background-process

我有三个shell脚本说A,B和C.我需要在后台运行A并在后台运行B,直到A在后台完成执行。同样在前台运行C,直到A和B完成执行。

我之前以这种方式为2个进程执行此操作。

./A.sh &  
while ps -p $! >/dev/null; do   
./B.sh  
done 

我需要在后台运行B,在前台运行C,直到A在后台完成执行。如何修改上述代码。

2 个答案:

答案 0 :(得分:1)

这将运行A& B在后台,前景为C; B& C将循环直到A结束:

#!/bin/bash

./A.sh &
APID=$!

while ps -p ${APID} >/dev/null; do
    ./B.sh & ./C.sh
done

我的盒子中的例子:

[ 09:39 jon@hozbox.com ~/SO/bash ]$ cat A.sh
#!/bin/bash
echo "A Started at: `date`"
sleep 30
echo "A Finished at: `date`"

[ 09:39 jon@hozbox.com ~/SO/bash ]$ cat B.sh
#!/bin/bash
echo "B Started at: `date`"
sleep 10
echo "B Finished at: `date`"

[ 09:39 jon@hozbox.com ~/SO/bash ]$ cat C.sh
#!/bin/bash
echo "C Started at: `date`"
sleep 5
echo "C Finished at: `date`"

[ 09:38 jon@hozbox.com ~/SO/bash ]$ ./how-to-program-in-this-bash-script-of-background-processes.sh
A Started at: Wed Nov 23 09:38:39 PST 2011
C Started at: Wed Nov 23 09:38:39 PST 2011
B Started at: Wed Nov 23 09:38:39 PST 2011
C Finished at: Wed Nov 23 09:38:44 PST 2011
C Started at: Wed Nov 23 09:38:44 PST 2011
B Started at: Wed Nov 23 09:38:44 PST 2011
B Finished at: Wed Nov 23 09:38:49 PST 2011
C Finished at: Wed Nov 23 09:38:49 PST 2011
C Started at: Wed Nov 23 09:38:49 PST 2011
B Started at: Wed Nov 23 09:38:49 PST 2011
B Finished at: Wed Nov 23 09:38:54 PST 2011
C Finished at: Wed Nov 23 09:38:54 PST 2011
C Started at: Wed Nov 23 09:38:54 PST 2011
B Started at: Wed Nov 23 09:38:54 PST 2011
B Finished at: Wed Nov 23 09:38:59 PST 2011
C Finished at: Wed Nov 23 09:38:59 PST 2011
C Started at: Wed Nov 23 09:38:59 PST 2011
B Started at: Wed Nov 23 09:38:59 PST 2011
B Finished at: Wed Nov 23 09:39:04 PST 2011
C Finished at: Wed Nov 23 09:39:04 PST 2011
C Started at: Wed Nov 23 09:39:04 PST 2011
B Started at: Wed Nov 23 09:39:04 PST 2011

A Finished at: Wed Nov 23 09:39:09 PST 2011
B Finished at: Wed Nov 23 09:39:09 PST 2011
C Finished at: Wed Nov 23 09:39:09 PST 2011
[ 09:39 jon@hozbox.com ~/SO/bash ]$


如果同时要求AB,那么请等一下$!A的pid:

./B.sh & ./A.sh &
while ps -p $! >/dev/null; do   
./C.sh  
done 

以下是我的方框中的一个例子:

[ 19:08 jon@hozbox.com ~ ]$ date
Tue Nov 22 19:08:19 PST 2011
[ 19:08 jon@hozbox.com ~ ]$ sleep 15 & sleep 20 &
[1] 1126
[2] 1127
[ 19:08 jon@hozbox.com ~ ]$ while ps -p $! > /dev/null; do sleep 1 && date; done
Tue Nov 22 19:08:26 PST 2011
Tue Nov 22 19:08:27 PST 2011
Tue Nov 22 19:08:28 PST 2011
Tue Nov 22 19:08:29 PST 2011
Tue Nov 22 19:08:31 PST 2011
Tue Nov 22 19:08:32 PST 2011
Tue Nov 22 19:08:33 PST 2011
Tue Nov 22 19:08:34 PST 2011
Tue Nov 22 19:08:35 PST 2011
Tue Nov 22 19:08:36 PST 2011
Tue Nov 22 19:08:37 PST 2011
Tue Nov 22 19:08:38 PST 2011
[1]-  Done                    sleep 15
Tue Nov 22 19:08:39 PST 2011
Tue Nov 22 19:08:40 PST 2011
Tue Nov 22 19:08:41 PST 2011
Tue Nov 22 19:08:42 PST 2011
Tue Nov 22 19:08:43 PST 2011
[2]+  Done                    sleep 20
Tue Nov 22 19:08:44 PST 2011
[ 19:08 jon@hozbox.com ~ ]$

答案 1 :(得分:0)

从你的图片看,你看起来你希望B和C在A运行时循环,然后在A完成后杀死它们。这是代码:

# Run A in the background and get its process ID
./A.sh &
PIDA = $!

# Loop B in the background and get its process ID
while ps $PIDA >/dev/null 2>&1; do
    ./B.sh
done &
PIDB = $!

# Loop C like B, get its PID
while ps $PIDA >/dev/null 2>&1; do
    ./C.sh
done &
$PIDC = $!

# Wait until A finishes, then kill B and C
wait $PIDA
ps $PIDB >/dev/null 2>&1 && kill $PIDB
ps $PIDC >/dev/null 2>&1 && kill $PIDC