当前位置: 动力学知识库 > 问答 > 编程问答 >

linux - check if file exist and wait for a sh script to run until the file is found

问题描述:

I'm working on a linux server which is sometimes very slow. So when i add some jobs to run for me i have to wait for a few hours just to run a simple calculation.

I was wondering if i am able to start the next analysis but let it wait until the output of the previous analysis is there. (the second analysis needs the first analysis output)

I tried to make except and other options working but still no success (found except and other options in previous question on stackoverflow):

expect {

'output/analysis_file1.txt'

}

Any ideas/hints are appreciated and will help me allot.

The only thing i want is to let the second scrip wait till the text file of the first script is given.

The 4 scripts:1.

#!/bin/bash

#$ -cwd

./script1.sh

. ./script2.sh $repla

. ./script3.sh $replac

2:

repla=''

for i in 'abcdefghijklmnopqrst'

do

repla=`echo $i | sed 's/'abc'/'xyz'/g'`

#echo $repla

done

3:

replac=''

for j in $1

do

replac=`echo $j | sed 's/'xyz'/'san'/g'`

#echo $replac

done

4:

replace=''

for h in $1

do

replace=`echo $h | sed 's/'san'/'sander'/g'`

#echo $replace

done

网友答案:

You can use wait if you know the pid of the process running in background. Wait will also return the same exit code of the process it is waiting to stop.

firstProcess &  # Running in background
firstPid=$!
otherProcess    # Concurrent with firstProcess
wait $firstPid  # Wait firstProcess finish
anotherProcess
网友答案:

you can use below core with some modifications

#!/bin/bash
while  [  ! -f FILE_NAME ]
  do
     sleep SOME_SECONDS
  done
echo "file found"
网友答案:

Instead of executing multiple scripts independently you should create a master script runner like this:

#!/bin/bash

# sanity checks & parse arguments

./script1
ret=$?
# check for return value of script1 using $ret variable

./script2
ret=$?
# check for return value of script2 using $ret variable

./script3
ret=$?
# check for return value of script3 using $ret variable

...
# do cleanup and reporting
分享给朋友:
您可能感兴趣的文章:
随机阅读: