스크립트: 첫 번째 프로세스가 끝나면 두 번째 프로세스를 실행합니다.

스크립트: 첫 번째 프로세스가 끝나면 두 번째 프로세스를 실행합니다.

두 부분으로 구성된 스크립트를 만들려고 합니다. 첫 번째는 초기 작동 상태이고 두 번째는 평형 시스템입니다. 문제는 두 번째 프로세스가 첫 번째 프로세스에 종속되어 있기 때문에 두 번째 프로세스를 실행하기 전에 첫 번째 프로세스를 종료해야 한다는 것입니다. 어떻게 해야 하나요?

내가 실행중인 코드는 다음과 같습니다.

#!bin/sh

echo -n  "Imput Chemical Potential:"
read chem
awk < towhee_input -v n="-$chem" 'NR==12 {$0=n}{print}'> towhee_input1
echo -n  "Imput nstep initial:"
read nstepi
r=5
declare -i p
pi=$nstepi/$r
awk < towhee_input1 -v k="$nstepi" 'NR==18 {$0=k}{print}'> towhee_input2
awk < towhee_input2 -v t="$pi" 'NR==22 {$0=t}{print}'> towhee_input3
awk < towhee_input3 -v t="$pi" 'NR==24 {$0=t}{print}'> towhee_input4
awk < towhee_input4 'NR==82 {$0=".true."}{print}'> towhee_input5

##### (i wont that process finish first and then continue the script)
towhee towhee_input5 > output &  
##### (i wont that process finish first and then continue the script)

cp towhee_final towhee_initial
echo "Run again?"
read wwwww

u=5
declare -i nstep
nstep=$nstepi*$u
declare -i p
p=$pi*$u
awk < towhee_input5 -v k="$nstep" 'NR==18 {$0=k}{print}'> towhee_input6
awk < towhee_input6 -v t="$p" 'NR==22 {$0=t}{print}'> towhee_input7
awk < towhee_input7 -v t="$p" 'NR==24 {$0=t}{print}'> towhee_input8
awk < towhee_input8 'NR==82 {$0=".false."}{print}'> towhee_input9
towhee towhee_input9 > towhee.prod &

어떻게 해야 하나요?

답변1

&마지막 것만 제거하세요.

towhee towhee_input5 > output &

쉘에서는 &백그라운드에서 실행을 의미합니다. 프로세스가 포그라운드에서 실행되도록 하려면 해당 프로세스를 제거하면 스크립트가 끝나면 스크립트가 실행됩니다.

편집하다

백그라운드에서 명령을 실행하고 기다리려면 다음을 사용하십시오.wait

towhee towhee_input5 > output &
wait
continue your code...

이 코드는 이전에 백그라운드에서 시작한 모든 명령을 기다리지만 가장 최근 백그라운드 명령의 PID를 포함하는 변수로 wait $!사용할 수도 있으며 bash 내장 명령은 일반적으로 wait를 사용합니다. PID를 인수 특정 프로세스로 사용합니다.$!wait

관련 정보