简体   繁体   English

在 Azure 上启动时在 Linux 实例上运行初始化脚本

[英]Running init script on a Linux instance at launch on Azure

I'm launching a Linux VM on Azure (eg Ubuntu Server) and want to execute my init script at instance launch.我正在 Azure 上启动 Linux VM(例如 Ubuntu Server),并希望在实例启动时执行我的 init 脚本。

I want to get the same behavior as with AWS User Data .我想获得与AWS User Data相同的行为。

How can I do that without creating my custom VM image?如何在不创建自定义 VM 映像的情况下做到这一点?

You can use Custom Script For Linux extension from Microsoft for any scripting post provisioning of the VM.您可以使用 Microsoft 的Custom Script For Linux扩展来进行 VM 的任何脚本后置备。

Documentation can be found here .文档可以在这里找到

Under "Custom data" one can add something like: (assuming Ubuntu OS)在“自定义数据”下,可以添加如下内容:(假设 Ubuntu 操作系统) 在此处输入图像描述

#!/bin/bash
sudo su
sudo apt update -y  #-y means yes to all prompts
sudo apt install apache2 -y
sudo systemctl status apache2
rm /var/www/html/index.html
cat <<EOF >/var/www/html/index.html
 
<html><body><h1> <font color="blue"> <center>Webserver #01<center></font></h1></body></html>
<html><body><h1> <font color="blue"> <center>Welcome to Microsoft Azure Administrator AZ-104 Training Bootcamp !<center></font></h1></body></html>
<style>
.aligncenter {
    text-align: center;
}
</style>
EOF

NOTE-Pls refer my other answer(Custom data) which is a simpler way of doing this.注意-请参考我的其他答案(自定义数据),这是一种更简单的方法。
As recommended,option 2 is to use Linux extension:按照建议,选项 2 是使用 Linux 扩展: 在此处输入图像描述

Select extension>>Custom script from Linux(by Microsoft)选择扩展>>来自 Linux 的自定义脚本(由 Microsoft 提供) 在此处输入图像描述

在此处输入图像描述

Need to select a .sh file>>we need to upload it into azure storage account需要选择一个 .sh 文件>>我们需要将它上传到 azure storage account 在此处输入图像描述

Make sure the file name is updated in the command:确保在命令中更新文件名:

在此处输入图像描述

There are multiple ways to run a script in linux:在 linux 中运行脚本有多种方法:
-Sh script-name.sh -sh 脚本名称.sh
-Bash script-name.sh -Bash 脚本名称.sh
-Simply run file ie - 只需运行文件即

path/script-name.sh

If in current directory,如果在当前目录中,

./script-name.sh

========================================================================= Generating a bash script in windows: ==================================================== =======================在windows中生成bash脚本:
Ref https://www.howtogeek.com/261591/how-to-create-and-run-bash-shell-scripts-on-windows-10/参考https://www.howtogeek.com/261591/how-to-create-and-run-bash-shell-scripts-on-windows-10/
Windows comes with Linux integration. Windows 带有 Linux 集成。

  1. Open cmd打开命令
  2. Type 'bash'输入“bash”
  3. This should open linux prompt.这应该会打开 linux 提示符。 If not,follow steps here: https://www.howtogeek.com/249966/how-to-install-and-use-the-linux-bash-shell-on-windows-10/如果没有,请按照此处的步骤操作: https ://www.howtogeek.com/249966/how-to-install-and-use-the-linux-bash-shell-on-windows-10/
  4. Following this: https://www.guru99.com/introduction-to-shell-scripting.html i.在此之后: https ://www.guru99.com/introduction-to-shell-scripting.html i。 Create a file using a vi editor(or any other editor).使用 vi 编辑器(或任何其他编辑器)创建文件 Name script file with extension .sh ii.使用扩展名 .sh 命名脚本文件 ii。 Start the script with #!使用#!开始脚本/bin/sh iii. /bin/sh三。 Write some code.写一些代码。 iv. iv. Save the script file as filename.将脚本文件另存为文件名。 sh

NOTE:For executing the script within linux type bash filename.sh ( don't do this for a startup script created,don't want it to run on current machine )注意:要在 linux 中执行脚本,请键入bash filename.sh不要为创建的启动脚本执行此操作,不要让它在当前机器上运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM