【问题标题】:Shell script to check for a file at regular period定期检查文件的 Shell 脚本
【发布时间】:2016-11-14 11:03:04
【问题描述】:

我是 Shell 脚本的新手。请帮忙。

进程正在特定位置创建触发器文件。我想检查该文件是否已创建。所以我正在编写一个 shell 脚本,可以安排在进程估计结束时间前 15 分钟运行。

        function go()
        {

            ssh -o StrictHostKeyChecking=no $SSH_User_IP      
            scp $PMROOT/NACO/SrcFiles/SDS/sds/sds_interface_core.trg $SSH_User_IP:$PMROOT_Fload/td_import/interface/sds/sds_interface_core.trg
            sleep 3                                                                                     
        }
        while true 
        do 
            go 
        done

我必须检查的位置在我必须执行 ssh 的另一台 unix 机器上。所以我需要的帮助是将事情放在一起,以便我的脚本可以通过 ssh 连接并每 1 分钟检查一次触发器文件。

【问题讨论】:

    标签: bash shell unix ssh


    【解决方案1】:

    Ssh 返回远程命令的退出代码。这应该有效:

    while true; do
        ssh username@remotehost "[ -e /path/to/file/to/check ]"
        if [ $? -eq 0 ]; then
            echo "file exists!"
            break
        fi
        sleep 60
    done
    

    汉努

    【讨论】:

      【解决方案2】:

      这个怎么样?

      while true; do
          ssh hostname "test -f /some/file" && { echo "file exists"; break; }
          sleep 60
      done
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-05
        • 1970-01-01
        • 2011-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多