スクリプトでCURLコマンドを一度だけ実行しますか?

スクリプトでCURLコマンドを一度だけ実行しますか?

説明するには、現在のフォルダに変更があるかどうかを監視し、変更が検出されたら、検出されたファイルをrsyncを介して自分のサーバーにアップロードするだけです。これは問題なく動作します:

#!/bin/bash
time_stamp=$(date +"%B-%d-%Y")
inotifywait -mr /usr/lib/unifi-video/data/videos -e create -e moved_to |
  while read path action file; do
  echo "The file '$file' appeared in directory '$path' via '$action'"
  rsync -avz -e "ssh -p 221" /$path/$file [email protected]:~/"$time_stamp"/ 
done

ここでほとんどのスクリプトを見つけました。フォルダ内の新しいファイルを監視するスクリプト?

問題:上記のスクリプトに次のCURL行を追加しようとしましたが、複数のファイルが同時に検出されるため、CURL行も複数回実行されます。複数のファイルが検出されたときにCURL行が複数回実行されるのを防ぐ方法を見つけようとしています。

curl http://textbelt.com/text -d number=XXXXXXX -d "message=Motion Detected";

私はrsyncコマンドの下に新しい行として直接追加し、rsyncコマンドの後に&&を使ってみました。どちらの方法も CURL コマンドを複数回実行します。

私が試したことの例:

#!/bin/bash
time_stamp=$(date +"%B-%d-%Y")
inotifywait -mr /usr/lib/unifi-video/data/videos -e create -e moved_to |
  while read path action file; do
  echo "The file '$file' appeared in directory '$path' via '$action'"
  rsync -avz -e "ssh -p 221" /$path/$file [email protected]:~/"$time_stamp"/ 
  curl http://textbelt.com/text -d number=XXXXXXX -d "message=Motion Detected";
done

出力例:

The file 'test30' appeared in directory '/usr/lib/unifi-video/data/videos/' via 'CREATE'
sending incremental file list

sent 39 bytes  received 11 bytes  20.00 bytes/sec
total size is 0  speedup is 0.00

{
"success": true
}

The file 'test31' appeared in directory '/usr/lib/unifi-video/data/videos/' via 'CREATE'
sending incremental file list

sent 39 bytes  received 11 bytes  20.00 bytes/sec
total size is 0  speedup is 0.00

{
 "success": true
}

2つの「成功」行は、各検出とアップロードの後に​​CURLコマンドが2回実行されたことを示します。

情報を追加するのを忘れた場合はお知らせください...

ベストアンサー1

すべてのアップデートを実行するrsyncのも無駄です。次のスクリプトは、最後のイベント以降0.1秒間アクティビティがない場合にrsync一度実行されます。curl

#!/bin/bash
time_stamp=$(date +"%B-%d-%Y")
inotifywait -mr /usr/lib/unifi-video/data/videos -e create -e moved_to |
while true; do
  T=''
  while read $T path action file; do
    echo "The file '$file' appeared in directory '$path' via '$action'"
    T='-t 0.1'
  done
  rsync -avz -e "ssh -p 221" /usr/lib/unifi-video/data/videos/ [email protected]:~/"$time_stamp"/ 
  curl http://textbelt.com/text -d number=XXXXXXX -d "message=Motion Detected"
done

おすすめ記事