简体   繁体   中英

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.

I want to get the same behavior as with AWS User Data .

How can I do that without creating my custom VM image?

You can use Custom Script For Linux extension from Microsoft for any scripting post provisioning of the VM.

Documentation can be found here .

Under "Custom data" one can add something like: (assuming Ubuntu OS) 在此处输入图像描述

#!/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: 在此处输入图像描述

Select extension>>Custom script from Linux(by Microsoft) 在此处输入图像描述

在此处输入图像描述

Need to select a .sh file>>we need to upload it into azure storage account 在此处输入图像描述

Make sure the file name is updated in the command:

在此处输入图像描述

There are multiple ways to run a script in linux:
-Sh script-name.sh
-Bash script-name.sh
-Simply run file ie

path/script-name.sh

If in current directory,

./script-name.sh

========================================================================= Generating a bash script in windows:
Ref https://www.howtogeek.com/261591/how-to-create-and-run-bash-shell-scripts-on-windows-10/
Windows comes with Linux integration.

  1. Open cmd
  2. Type 'bash'
  3. This should open linux prompt. If not,follow steps here: 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. Create a file using a vi editor(or any other editor). Name script file with extension .sh ii. Start the script with #!/bin/sh iii. Write some code. 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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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