All Products
Search
Document Center

:What do I do if 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?

Last Updated:Jan 29, 2024

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 configure user data for a Linux instance and restart the instance, the configured user data does not take effect and the following error message appears in the system logs: Failed to run module scripts-user(scripts in /var/lib/cloud/instance/scripts). The error message indicates that the user data script failed to run in cloud-init on instance startup.

image.png

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 use various scripts or files as user data scripts for the Linux instance. The scripts have different characteristics and are subject to different user data conventions. Troubleshoot the user data script of the Linux instance based on the user data conventions that apply to the script.

User-Data scripts

User-data scripts are directly executed as shell scripts after they are passed into Linux instances. User-data scripts have the following characteristics:
  • The first line starts with a number sign and an exclamation mark (#!).
  • User-data scripts are run once only the first time the instance starts.
Example:
#!/bin/sh
echo "Hello World. The time is now $(date -R)!" | tee /root/userdata_test.txt

The example user-data script can be run to write the system time to the userdata_test.txt file the first time the instance starts.

Note

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

Important Cloud-config is the simplest method to 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 is a convenient way to pre-configure specific services (such as YUM repository update, SSH key import, and dependency installation) for instances. Cloud-config data has the following characteristics:
  • The first line starts with #cloud-config, and the header cannot include spaces.
  • The script must follow the YAML syntax.
  • The frequency at which the user data is run varies based on your configured modules. 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.txt

The example cloud-config data can be run to modify the default software repository and write the latest system time to the userdata_test.txt file each time the instance starts.

Include files

An include file contains one or more script links, one per line. When the instance starts, cloud-init reads each script link and script content. If an error occurs while a script is being read, the remaining scripts are not read. Include files have the following characteristics:
  • The first line starts with #include, and the header cannot include spaces.
  • Each script cannot exceed 16 KB in size before it is encoded in Base64.
  • The frequency at which the user data is run varies based on the types of scripts and modules.
Example:
#include
https://ecs-image-test.oss-cn-hangzhou.aliyuncs.com/userdata/myscript.sh

The example include file contains a script link. The running frequency is determined by the type of the script. For example, if the script is a user-data script, the script is run once only the first time the instance starts.

Gzip compressed content

If your user-data script, cloud-config data, or include file may exceed 16 KB in size, you can gzip compress the user-data script, cloud-config data, or include file to the .gz format, create a link to the gzip compressed content, and then pass in the link as an include file. cloud-init automatically decompresses the gzip compressed content. The result of running the decompressed content shows no difference from that of running a script that is directly passed in. Gzip compressed content has the following characteristics:
  • The first line starts with #include, and the header cannot include spaces.
  • The size of the gzip compressed content cannot exceed 16 KB before it is encoded in Base64.
  • The frequency at which the user data is run varies based on the types of scripts and modules.
Example:
#include
https://ecs-image-test.oss-cn-hangzhou.aliyuncs.com/userdata/myscript.gz

The example include file contains a link to gzip compressed content. cloud-init reads the gzip compressed content and automatically decompresses and runs it. The running frequency is determined by 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 the instance starts.

Upstart Job

The content of upstart job scripts is placed into a file in the /etc/init directory. Upstart job scripts have the following characteristics:
  • The first line starts with #upstart-job, and the header cannot include spaces.
  • Upstart job scripts are run each time the instance starts.
Note 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] #Starts at run levels 2, 3, 4, and 5.
stop on runlevel [!2345] #Stops at run levels other than 2, 3, 4, and 5.
exec echo "Hello World. The time is now $(date -R)!" | tee /root/output.txt

For sample user data, see Manage the user data of Linux instances.