【问题标题】:Check every half and hour and see whether the if condition is true or not每半小时检查一次,看看 if 条件是否为真
【发布时间】:2012-09-16 01:09:39
【问题描述】:

有两个目录,其中包含一些文件。我正在使用 Hadoop,这两个目录都是 Hadoop 文件系统。

目录的第一个位置-

/sys/edw/

目录的第二个位置-

/apps/only/

我想看看these two directories 是否有一些files or not。如果它们包含一些文件,则continue and execute my other shell scripts,但如果它们不包含任何文件,则wait for half an hour,然后再次检查文件是否已到达这两个目录。

如果已经到达,则执行其他shell脚本,如果没有到达,则再次等待半小时,并继续每半小时检查一次。

算法-

If [ hadoop fs -ls /sys/edw/   AND hadoop fs -ls /apps/only/ ]
then
continue executing my other shell scripts
but if condition is not true
wait for half an hour and then check again with the if loop and keep on looking every half an hour until the if condition is not true

我只想在上面的 if 条件为真时执行下面的脚本。

query.sh

基本上我有几个脚本我只想运行if condition is true 否则继续检查every half an hour 并查看if condition is true or not 是否。

我在奔跑SunOS 5.1

更新代码:-

我正在尝试类似下面的操作,并且此目录中不存在文件(/apps/hdmi/20120916),因此它应该休眠半小时,但它不会休眠,而是执行其他 shell 脚本它不应该这样做-

while [ ! hadoop fs -ls /apps/hdmi/20120916 ]; do
    sleep 1800
done

echo "Hello new11"
pwd

我做错了什么?

再次更新:-

while ! hadoop fs -ls /apps/hdmi/20120916 ; do
    sleep 1800
done

echo "Hello new11"
pwd

我也尝试过上面的那个,但是这个目录不存在(/apps/hdmi/20120916),而不是睡半个小时,它会打印不应该发生的Hello new11。它应该等待半个小时。我还缺少什么吗?我正在运行SunOS 5.1

【问题讨论】:

  • 阅读man croncron 可让您执行定期任务,非常适合这项工作。
  • @nneonneo 将其作为答案,以便他接受。
  • 实际上,让我直截了当——您希望脚本在条件为真后退出并停止检查?
  • 一旦条件成立,我需要执行query.sh script 否则继续每半小时检查一次,直到条件成立。
  • 去掉括号:while ! hadoop fs -ls /apps/hdmi/20120916; do ...

标签: linux bash shell unix


【解决方案1】:

如果你不想使用cron,那么你可以使用无限循环:

while true; do
  if hadoop fs -ls /apps/hdmi/20120916; then
    query.sh # call other script
    break # exit loop
  else
    sleep 1800
  fi
done

希望我正确理解了您的要求。

【讨论】:

    【解决方案2】:

    您不能使用while ! COMMAND; do! 在这种情况下不是否定运算符。您要做的是执行命令hadoop fs -ls /DIR 并在命令未列出任何文件时休眠。

    为了执行命令,把它放在反引号``之间。

    `hadoop fs -ls /apps/hdmi`
    

    使用test 或方括号[ ] 来评估命令的输出。

    [ `hadoop fs -ls /apps/hdmi` ]
    

    如果命令生成非零字符串(即目录中有文件时),则表达式将计算为 true。由于这与您想要的相反(在有 no 文件时继续),您需要否定表达式。

    [ ! `hadoop fs -ls /apps/hdmi` ]
    

    因此您的脚本应该看起来像这样:

    while [ ! `hadoop fs -ls /apps/hdmi` ]; do
      sleep 1800
    done
    
    echo "Hello new11"
    
    pwd
    

    这是假设hadoop fs -ls在目录中没有文件时不产生任何输出。

    【讨论】:

      【解决方案3】:

      如果您希望脚本在条件变为真后退出,只需使用 sleep 1800 让脚本休眠 1800 秒(半小时),然后使用 while 循环:

      while [ ! condition ]; do
          sleep 1800
      done
      
      # execute other scripts since condition is now true
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-19
        • 1970-01-01
        相关资源
        最近更新 更多