This topic describes the cause of and solution to the following issue: The user data of a Linux instance does not take effect, and the "Failed to run module scripts-user(scripts in /var/lib/cloud/instance/scripts)" error message appears in the system logs.
Problem description
After you set instance user data for a Linux instance and restart the instance, the instance user data does not take effect. The system log contains the following error message: "Failed to run module scripts-user(scripts in /var/lib/cloud/instance/scripts)". This message indicates that the user-data script in cloud-init failed to run when the instance started.

For information about how to view the system logs and screenshots of an instance, see View system logs and screenshots.
Cause
This issue may occur due to errors in the user data script that was passed into the Linux instance, such as an invalid syntax format or an error that occurred while running the user data script.
Solution
You can configure instance user data for a Linux instance using different types of scripts, each with its own features and format requirements. To troubleshoot the issue, check your script content against the required format for the script type you are using.
User-Data scripts
User-data scripts are directly run as shell scripts after the scripts are passed into Linux instances. User-data scripts have the following characteristics:
The first line starts with a combination of a number sign and an exclamation point (
#!).User-data scripts are run once only on the first boot of an instance.
Example:
#!/bin/sh
echo "Hello World. The time is now $(date -R)!" | tee /root/userdata_test.txtThe first time an instance starts, you can run the sample user-data script can be run to write the system time to the userdata_test.txt file.
If a User-Data script fails to run, you can run the following common Cloud Assistant command to obtain the error logs about the failure: ACS-ECS-UserData-Check-for-linux.sh. If an error message appears in the logs, an error occurs while running the script. If an error message does not appear in the logs, the script runs as expected. Check the script from other aspects to identify the cause of the issue. For information about common Cloud Assistant commands, see View and run common commands.
Cloud Config data
cloud-config allows you to easily perform specific tasks by using user data. When you use cloud-init to perform specific tasks, we recommend that you use cloud-config to complete configurations.
cloud-config allows you to pre-configure specific services, such as Yellowdog Updater Modified (YUM) repository update, SSH key import, and dependency installation, for instances in a convenient manner. cloud-config data has the following characteristics:
The first line is
#cloud-config, and the header cannot include spaces.cloud-config data must follow the YAML syntax.
The frequency at which the user data is run varies based on the configured module. For example, if you configure the Apt Configure module, the user data is run only once for each instance. If you configure the Bootcmd module, the user data is run each time the instance starts.
Example:
#cloud-config
apt:
primary:
- arches: [default]
uri: https://us.archive.ubuntu.com/ubuntu/
bootcmd:
- echo "Hello World. The time is now $(date -R)!" | tee /root/userdata_test.txtYou can run the sample cloud-config data to modify the default software repository and write the latest system time to the userdata_test.txt file each time an instance starts.
Include files
An include file contains one or more script links. Each line contains one script link. When an instance starts, cloud-init reads each script link and script content. If an error occurs when the content of a script is being read, the remaining scripts are not read. Include files have the following characteristics:
The first line is
#include, and the header cannot include spaces.Each script cannot exceed 16 KB in size before the script is encoded in Base64.
The frequency at which the user data is run varies based on the script type and module type.
Example:
#include
https://ecs-image-test.oss-cn-hangzhou.aliyuncs.com/userdata/myscript.shThe sample include file contains a script link. The running frequency varies based on the script type. For example, if the script is a user-data script, the script is run once only the first time an instance starts.
Gzip compressed content
If the user-data script, cloud-config data, or include file exceeds 16 KB in size, you can compress the user-data script, cloud-config data, or include file into gzip compressed content (.gz), create a link to the content, and then pass the link in an include file. cloud-init automatically decompresses the gzip compressed content. The result of running the decompressed content is the same as the result of running uncompressed content that is directly passed. Gzip compressed content has the following characteristics:
The first line is
#include, and the header cannot include spaces.The size of the gzip compressed content cannot exceed 16 KB before the content is encoded in Base64.
The frequency at which the user data is run varies based on script type and module type.
Example:
#include
https://ecs-image-test.oss-cn-hangzhou.aliyuncs.com/userdata/myscript.gzThe sample include file contains a link to gzip compressed content. cloud-init reads the gzip compressed content and automatically decompresses and runs the content. The running frequency varies based on the script type. For example, if the gzip compressed content is obtained by compressing a user-data script, the gzip compressed content is run once only the first time an instance starts.
Upstart Job
The content of upstart job scripts is stored in a file in the /etc/init directory. Upstart job scripts have the following characteristics:
The first line is
#upstart-job, and the header cannot include spaces.Upstart job scripts are run each time the instance starts.
To use upstart job scripts, you must install the upstart service for the instance. The upstart service is supported for instances that run one of the following operating systems: CentOS 6, Ubuntu 10, Ubuntu 12, Ubuntu 14, Debian 6, and Debian 7.
Example:
#upstart-job
description "upstart test"
start on runlevel [2345] #Start at run levels 2, 3, 4, and 5.
stop on runlevel [!2345] #Stop at run levels other than 2, 3, 4, and 5.
exec echo "Hello World. The time is now $(date -R)!" | tee /root/output.txtFor more examples of instance user data, see Use instance user data (Linux instances).