简体   繁体   English

计时器检查文件bash外壳的结尾

[英]timer to check the end of the file bash shell

I am using a code basically runs to restart a calculation after one finishes. 我正在使用的代码基本上运行以在完成后重新开始计算。 The code was working fine. 代码工作正常。 But i wanna improvise the code. 但我想即兴编写代码。 Right now it runs on a timer function sleep. 现在,它在定时器功能sleep上运行。 It never checks for the end of the file. 它从不检查文件的结尾。 So it waits 22 min to submit the next job irrespective of the previous job. 因此,无论先前的作业如何,它都要等待22分钟才能提交下一个作业。 So i want to change the code in such a way it checks the end of the file every one minute for the keyword "End of program" from the log file and submits the next job. 因此,我想以这样一种方式更改代码,即每隔一分钟检查日志文件中关键字“程序结束”的文件结尾并提交下一个作业。 Please drop me a msg if you have any ideas ... 如果您有任何想法,请给我留言。


while ( 1 ) 而(1)


#sleep for X seconds/minutes #sleep X秒/分钟

    $SLEEP 22m
    #cut JobID into peeces:
    #e.g.: 31380.chic1i20.informatik.tu-chemnitz.de Info: Your job definition is checked against CHiC policies. --> 31380.chic1i20
    #set JOBnr = `echo $JOBID | $CUT -c-15`
    set JOBnr = `echo $JOBID | $CUT -d " " -f1 | $CUT -d "." -f-2`
    $QSTAT -nu "$USER" > .qstat_outfile
    if ( "$?" ) then
            echo "autorestart: qstat error"
            $CAT .qstat_outfile
            continue
    endif

Try this: 尝试这个:

UNWRITTEN=0
while [ $UNWRITTEN -eq 0 ]; do
 token=$(tail -n 2 .qstat_outfile)
 echo $token
 if [[ $token == "End of program" ]]
 then
     echo "autorestart..."
     UNWRITTEN=1
 fi
 sleep 60
done

Explanation: 说明:

  • Program continually loops until it find the correct line at the end of the logfile. 程序不断循环,直到在日志文件末尾找到正确的行。 Then it restarts your calculations and exits. 然后,它重新启动计算并退出。
  • tail -n 2 reads the last two lines (which is actually the last line if there is a newline after "End of program". You may need to adjust this to -n 1 on your system.) tail -n 2读取最后两行(如果在“程序结束”之后有换行符,则实际上是最后一行。您可能需要在系统上将其调整为-n1。)

If I got you right, a while-loop like this may help you: 如果我说对了,这样的while循环可能会帮助您:

stop=0
while [ $stop == 0 ]; do 
    tail -1 $LOGFILE | grep -q "End of program" && stop=1 || sleep 60 
done

(It checks the last line ( tail -1 ) of LOGFILE for the presence of your search text, stops if text is found or sleeps for 60 seconds if not). (它会检查LOGFILE的最后一行( tail -1 )是否存在搜索文本,如果找到文本则停止搜索,否则睡眠60秒)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM