私のシェルスクリプトがなぜ機能しないのですか?

私のシェルスクリプトがなぜ機能しないのですか?

観察1

logic.sh

#!/bin/bash
#get system metrics
#do stuff and echo it
echo "put metrics" | nc $ip $port
echo "Metrics $metrics"

run_logic.sh

#!/bin/bash
while true;do
  sh logic.sh >> test.log 2>&1 &
  sleep 60
done

start_logic.sh

#!/bin/bash
case $1 in
  start)
       #start the run_logic.sh
       ;;
   stop)
      #stop the run_logic.sh
      ;;
    *)
     echo "Invalid Option!"
     exit 1
esac
exit0

観察2

logic.sh

#!bin/bash
while true;do
  #do stuff and echo it
  #get System Metrics and put it
  echo $stuff
  sleep 60
done

start_logic.sh

#!/bin/bash
case $1 in
  start)
       #do some stuff, check already started or not
       sh logic.sh >> test.log 2>&1 &
       ;;
   stop)
      #do some stuff
      #Kill the process
      ;;
    *)
     echo "Invalid Option!"
     exit 1
esac
exit0

今! 、観察1では、スクリプトが実行中に終了した。ログを確認しましたが、エラーメッセージが表示されませんでした。観察2では、スクリプトは正常に動作します(99%良い!)。それでは、観察1と観察2の違いは何ですか?最初のケースでは、スクリプトが死ぬのはなぜですか?

ベストアンサー1

logic.sh観察1では、バックグラウンドで60秒ごとに実行されるシェルを起動します(&)。使用されたコマンドはnetcatデータをIP /ポートに送信すると仮定しますが、リモート側(デフォルトではtcp)でリッスンしている人がいない場合はエラーで終了します。

おすすめ記事