linux - 为 Amazon linux AMI 添加服务启动脚本

标签 linux amazon-ec2 ec2-ami

我正在使用 Amazon Linux AMI 并对其进行一些自定义修改(添加 axis2server 等)并将其保存为新的 AMI。现在我要做的是在 AMI 启动时启动 axis2server(即 axis2server 应该在实例启动时自动启动)。为此,我使用了如下所示的初始化脚本并运行了以下命令:

chkconfig --add axisservice

但是当我从我的图像启动一个新实例时,axis2server 没有启动。

我只需要在启动时执行脚本/home/ec2-user/axis2-1.6.1/bin/axis2server.sh。我在这里错过了什么吗?

#! /bin/sh
# Basic support for IRIX style chkconfig
###
# chkconfig: 235 98 55
# description: Manages the services you are controlling with the chkconfig command
###

case "$1" in
  start)
        echo -n "Starting axisservice"        
        touch ~/temp.txt
        cd /home/ec2-user/axis2-1.6.1/bin
        ./axis2server.sh &
        echo "."
        ;;
  stop)
        echo -n "Stopping axisservice"
        echo "."
        ;;

  *)
        echo "Usage: /sbin/service axisservice {start|stop}"
        exit 1
esac

exit 0

我经历了https://help.ubuntu.com/community/CloudInit它也提供了一种称为用户数据脚本的机制,用户可以在其中启动脚本时执行脚本。

$ euca-run-instances --key mykey --user-data-file myscript.sh ami-axxxx

这是一个命令行选项,我想要的是当我通过 UI 启动实例时,应该启动脚本。因此,我认为上述选项不能用于我的情况。如果我错了,请纠正我。

谢谢,

最佳答案

我敢打赌环境没有设置(正确设置)。这意味着我猜测您的 shell 脚本试图启动另一个程序,但找不到它。

所以首先,我会调整您脚本的 start 部分(当前):

echo -n "Starting axisservice"        
touch ~/temp.txt
cd /home/ec2-user/axis2-1.6.1/bin
./axis2server.sh &
echo "."

编辑:

echo -n "Starting axisservice"        
touch ~/temp.txt
cd /home/ec2-user/axis2-1.6.1/bin
./axis2server.sh
RETVAL=$?
[ $RETVAL -eq 0 ] && echo Success
[ $RETVAL -ne 0 ] && echo Failure
echo "."

那我做了什么?

  • 删除了 & 以便脚本等待您的 shell 脚本 (axis2server.sh) 完成
  • 检查你的 shell 脚本的返回状态 ($?)

进一步调试:

set -x 添加到您的脚本中以启用跟踪并记录 stderrstdout

问题:

  1. 您是否知道 stop(在您的服务脚本中)没有执行任何操作?
  2. touch ~/temp.txt 是否应该创建 /root/temp.txt? (我猜测 root 运行这个脚本。)
  3. 如果我的建议都不起作用,您可以分享 axis2server.sh 并粘贴 stderrstdout 吗?

关于linux - 为 Amazon linux AMI 添加服务启动脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8388586/

相关文章:

linux - 默认 apt-get 语言更改

amazon-web-services - CDK如何使用预留的rds/ec2实例?

amazon-ec2 - vsftpd 错误列出目录

amazon-web-services - 使用 Jenkins 和 Cloud Formation 的 AWS AMI 自动化

amazon-ec2 - 创建 AMI 需要很长时间

linux - 检查 "cat"的输出是否不为空

c++ - Linux与Windows:执行速度

c - Linux - 如何将代码上传到主板上的专用飞思卡尔芯片 NIC?

amazon-web-services - AWS : Deploying a stack using Cloud Formation with an existing Key Pair

amazon-web-services - 如何在亚马逊 ec2 中更改实例类型