我编写了一个/etc/rc.local
脚本,它执行一个jar文件并在完成后自动关闭系统。该脚本似乎成功启动,但最终在完成之前关闭。
#!/bin/sh
#
mv --backup=t /tmp/rc.local.log /home/hadoop/logs
exec 2> /tmp/rc.local.log # send stderr from rc.local to a log file
exec 1>&2 # send stdout to the same log file
set -x # tell sh to display commands before execution
touch /var/lock/subsys/local
EC2_INSTANCE_ID="`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id || die \"wget instance-id has failed: $?\"`"
test -n "$EC2_INSTANCE_ID" || die 'cannot obtain instance-id'
EC2_AVAIL_ZONE="`wget -q -O - http://169.254.169.254/latest/meta-data/placement/availability-zone || die \"wget availability-zone has failed: $?\"`"
test -n "$EC2_AVAIL_ZONE" || die 'cannot obtain availability-zone'
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`"
##########
#run hadoop job
#############
su - hadoop -c 'java -jar /home/hadoop/my.jar'
##stop instance
/opt/aws/bin/ec2-stop-instances $EC2_INSTANCE_ID --region $EC2_REGION
知道我做错了什么吗?谢谢!
最佳答案
如果出现 exit 1 或更高错误,set -x 会自动关闭脚本。
如果我没记错的话,您的变量如 EC2_INSTANCE_ID 需要去掉双引号。这是因为双引号内的所有字符都采用文字值,除了 $
尝试
EC2_INSTANCE_ID= `wget -q -O - http://169.254.169.254/latest/meta-data/instance-id || die wget instance-id has failed: "$?"`
此外,我注意到您有不错的错误捕获命令,因此可以去掉 set -x ,这样您就可以在脚本退出之前看到遇到的错误。
我以前也从未见过 die 命令。
关于linux - bash - 在系统初始化时执行 jar 并正常关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24414277/