bash Aws Ec2 在启动时运行脚本程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49594391/
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
Aws Ec2 run script program at startup
提问by andrea cenciarelli
There is a method to setup an EC2 machine to execute Kafka starting script on startup? I use also java Aws SDK, so I accept both solution for a program java that run command on EC2 instance and solutions for a bash script mode that run kafka script at startup.
有没有一种方法可以设置 EC2 机器在启动时执行 Kafka 启动脚本?我也使用 java Aws SDK,所以我接受在 EC2 实例上运行命令的程序 java 的解决方案和在启动时运行 kafka 脚本的 bash 脚本模式的解决方案。
采纳答案by John Rotenstein
A script can be passed in the User Dataproperty.
可以在用户数据属性中传递脚本。
If you are using the Amazon Linux AMI, and the first line of the script begins with #!
, then the script will be executed the first time that the instance is started.
如果您使用的是 Amazon Linux AMI,并且脚本的第一行以 开头#!
,则脚本将在第一次启动实例时执行。
For details, see: Running Commands on Your Linux Instance at Launch
有关详细信息,请参阅:启动时在 Linux 实例上运行命令
回答by user9575749
What is the OS of EC2 instance?
EC2实例的操作系统是什么?
You could use userdata script at instance launch time. Remember this is just 1time activity
If your requirement is to start the script everytime you reboot EC2 instance then you could make use of
rc.local
file on Linux instances which is loaded at OS boot time.
您可以在实例启动时使用 userdata 脚本。请记住,这只是 1 次活动
如果您的要求是每次重新启动 EC2 实例时都启动脚本,那么您可以使用
rc.local
在操作系统启动时加载的 Linux 实例上的文件。
回答by N-ate
If you are running a windows EC2 you'll want to read: https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-user-data.html
如果您运行的是 Windows EC2,您需要阅读:https: //docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-user-data.html
Example:
例子:
<script>
echo Current date and time >> %SystemRoot%\Temp\test.log
echo %DATE% %TIME% >> %SystemRoot%\Temp\test.log
</script>