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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8388586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:08:14  来源:igfitidea点击:

Adding a service startup script for Amazon linux AMI

linuxamazon-ec2ec2-ami

提问by user915745

I am using an Amazon Linux AMI and doing some custom modifications(added an axis2server, etc) on it and saving it as a new AMI. Now what I want to do is when the AMI boots up, start up axis2server(ie.axis2server should automatically start when the instance boots up). For that I used a init script like below and ran the following command:

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

chkconfig --add axisservice

But when I launch a new instance from my image, the axis2server is not getting started.

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

I just only need to execute the script /home/ec2-user/axis2-1.6.1/bin/axis2server.sh at startup. Am I missing anything here?

我只需要在启动时执行脚本 /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 "" 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

I went through https://help.ubuntu.com/community/CloudInitas well and it provides a mechanism called User-Data Scripts, where a user can execute a script when launching the script.

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

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

This is a command line option and what I want is something like when I launch the instance through the UI, the script should be started.Therefore, I think the above option can not be used in my case. Please correct me if I am wrong.

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

Thanks, H.

谢谢,H。

回答by Till

I bet the environment is not set(up correctly). This means that I am guessing that your shell script tries to start another program and it's not to be found.

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

So at first, I'd adjust the startpart of your script (current):

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

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

Edited:

编辑:

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 "."

So what did I do?

那我做了什么?

  • removed &so script waits for your shell script (axis2server.sh) to complete
  • checked the return status ($?) of your shell script
  • 已删除,&因此脚本等待您的 shell 脚本 (axis2server.sh) 完成
  • 检查$?shell 脚本的返回状态 ( )

Further debugging:

进一步调试:

Add set -xto your scripts to enable tracing and log both stderrand stdout.

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

Questions:

问题:

  1. Are you are aware that stop(in your service script) doesn't do anything?
  2. touch ~/temp.txtis that supposed to create /root/temp.txt? (I'm guessingroot runs this script.)
  3. If none of my suggestions work, can you share axis2server.shand paste stderrand stdout?
  1. 您是否知道stop(在您的服务脚本中)不执行任何操作?
  2. touch ~/temp.txt那应该创造/root/temp.txt吗?(我root 运行这个脚本。)
  3. 如果没有我的建议工作,你可以分享axis2server.sh并粘贴stderrstdout