bash
1#!/bin/bash 2 3 4# get ec2 instance id 5instanceid=`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id` 6 7VC=`jps | grep 'application-0.0.1-SNAPSHOT.jar' | tr -s ' ' | cut -d ' ' -f 1 ` 8 9jstat=`jstat -gcutil $VC | tail -1 | tr -s ' ' | cut -c2-100` 10 11survivor0=`echo $jstat|cut -d ' ' -f 1` 12survivor1=`echo $jstat|cut -d ' ' -f 2` 13eden=`echo $jstat|cut -d ' ' -f 3` 14old=`echo $jstat|cut -d ' ' -f 4` 15permanent=`echo $jstat|cut -d ' ' -f 5` 16young=`echo $jstat|cut -d ' ' -f 6` 17youngtime=`echo $jstat|cut -d ' ' -f 7` 18fgc=`echo $jstat|cut -d ' ' -f 8` 19fgctime=`echo $jstat|cut -d ' ' -f 9` 20gct=`echo $jstat|cut -d ' ' -f 10` 21 22aws cloudwatch put-metric-data --metric-name "Survivor0" --namespace "Java/Tomcat" --dimensions "InstanceId=$instanceid" --value "$survivor0" --unit "Percent" 23 24aws cloudwatch put-metric-data --metric-name "Survivor1" --namespace "Java/Tomcat" --dimensions "InstanceId=$instanceid" --value "$survivor1" --unit "Percent" 25 26aws cloudwatch put-metric-data --metric-name "Eden" --namespace "Java/Tomcat" --dimensions "InstanceId=$instanceid" --value "$eden" --unit "Percent" 27 28aws cloudwatch put-metric-data --metric-name "Old" --namespace "Java/Tomcat" --dimensions "InstanceId=$instanceid" --value "$old" --unit "Percent" 29 30aws cloudwatch put-metric-data --metric-name "Permanent" --namespace "Java/Tomcat" --dimensions "InstanceId=$instanceid" --value "$permanent" --unit "Percent" 31 32aws cloudwatch put-metric-data --metric-name "YoungGC" --namespace "Java/Tomcat" --dimensions "InstanceId=$instanceid" --value "$young" --unit "Count" 33 34aws cloudwatch put-metric-data --metric-name "YoungGCTime" --namespace "Java/Tomcat" --dimensions "InstanceId=$instanceid" --value "$youngtime" --unit "Seconds" 35 36aws cloudwatch put-metric-data --metric-name "FullGC" --namespace "Java/Tomcat" --dimensions "InstanceId=$instanceid" --value "$fgc" --unit "Count" 37 38aws cloudwatch put-metric-data --metric-name "FullGCTime" --namespace "Java/Tomcat" --dimensions "InstanceId=$instanceid" --value "$fgctime" --unit "Seconds" 39 40aws cloudwatch put-metric-data --metric-name "TotalGCTime" --namespace "Java/Tomcat" --dimensions "InstanceId=$instanceid" --value "$gct" --unit "Seconds"
上記は、JavaVMのガベージコレクション周りの情報をCloudWatchに登録するスクリプトです。
それをcronで*/1 * * * * /home/ec2-user/gc.shに設定し、定期実行しています。
cronで実行しない場合、下記エラーが発生しませんが、cronで実行すると発生します。
一体原因はなぜでしょうか。ちなみに、awsのパスをフルパスにしましたが、同様のエラーが発生します。
(ec2-user) CMDOUT (/home/ec2-user/gc.sh: line 22: aws: command not found) (ec2-user) CMDOUT (/home/ec2-user/gc.sh: line 24: aws: command not found) (ec2-user) CMDOUT (/home/ec2-user/gc.sh: line 26: aws: command not found) (ec2-user) CMDOUT (/home/ec2-user/gc.sh: line 28: aws: command not found) (ec2-user) CMDOUT (/home/ec2-user/gc.sh: line 30: aws: command not found) (ec2-user) CMDOUT (/home/ec2-user/gc.sh: line 32: aws: command not found) (ec2-user) CMDOUT (/home/ec2-user/gc.sh: line 34: aws: command not found) (ec2-user) CMDOUT (/home/ec2-user/gc.sh: line 36: aws: command not found) (ec2-user) CMDOUT (/home/ec2-user/gc.sh: line 38: aws: command not found) (ec2-user) CMDOUT (/home/ec2-user/gc.sh: line 40: aws: command not found)
回答3件
あなたの回答
tips
プレビュー