Cloud Assistant vs. local shell: Command execution on Linux
This topic describes the differences between running commands on a Linux instance using Cloud Assistant and running them in a local shell.
Cloud Assistant overview
The Cloud Assistant Agent runs as a service on an ECS instance. It communicates directly with Alibaba Cloud services to retrieve and run commands, and then reports the execution results. The communication path for the Cloud Assistant Agent differs from that of a remote connection to an ECS instance, for example, over SSH. Consequently, the results of command execution can vary between the two methods. For more information about Cloud Assistant, see Cloud Assistant overview.
Differences in environment variables
The environment variables available during command execution differ between a Cloud Assistant session and a local shell session.
Cause
Local execution: When you connect to a Linux instance using a remote connection tool such as PuTTY or Xshell, or through a VNC remote connection from the ECS console, you start an interactive logon shell. In this environment, the shell interpreter, such as /bin/bash, loads configuration and startup files, such as /etc/profile, ~/.bash_profile, and ~/.bashrc. These files typically define various environment variables, such as HOSTNAME.
Cloud Assistant execution: When Cloud Assistant runs a command, it uses a non-interactive, non-login shell. This type of shell does not load startup files, such as /etc/profile, ~/.bash_profile, or ~/.bashrc. Therefore, Cloud Assistant cannot access the environment variables defined within these files.
If a command you run with Cloud Assistant depends on environment variables that are not available in its execution environment, the command may fail or produce unexpected results.
You can use the export command, either through Cloud Assistant or in a local session on the instance, to view the available environment variables.
Solution
To resolve this issue, modify your Cloud Assistant command to load the necessary startup files and define missing variables before executing the main command. Follow these steps:
-
Check whether the required environment variables exist. For more information about how to create a command in Cloud Assistant, see Create command.
Use the following command template to check whether a variable is set and to perform an action if it is not.
if [ -z "${<variable_name>+x}" ]; then <action_if_variable_is_not_set> fiFor example, to check for the $TERM variable, use the following Cloud Assistant command:
#!/bin/sh if [ -z "${TERM+x}" ]; then echo "\$TERM is not set when script executed via cloud assistant" else echo "\$TERM is set to '${TERM}'" fiIf the output is
$TERM is not set when script executed via cloud assistant, the $TERM variable is not defined in the environment. -
Load the required startup files, such as /etc/profile, ~/.bash_profile, or ~/.bashrc, within your command.
-
Method 1: Explicitly load startup files.
Add the
.command (dot) or the Bash-specificsourcecommand to the beginning of your shell script to load the necessary startup files. This method sources all variables from the file at once. The following examples demonstrate this:-
Use the
.command:#!/bin/sh . /etc/profile -
Use the
sourcecommand:#!/bin/bash source $HOME/.bashrc
-
-
Method 2: In the
#!statement at the beginning of a command, use a parameter to instruct the shell interpreter to load a startup file.NoteThis method is not recommended if the command output needs to be parsed or processed by another script, as it can produce extra diagnostic messages.
To have the shell interpreter automatically load startup files, you can enable its interactive and login modes. Use the
-iparameter for interactive mode and the-lparameter for login mode. When combined as-il, the shell behaves similarly to an SSH login session and loads the relevant profiles. Use the following shebang line:#!/bin/sh -ilIf you use the Bash interpreter, you can specify
#!/bin/bash -il. For more information, see the documentation for your specific shell interpreter. However, when you use the-iparameter, the shell expects its standard input (stdin) to be an interactive terminal to provide features like job control. Because Cloud Assistant runs commands with stdin connected to a null device (/dev/null), the shell fails this check and reports errors. Common error messages include:sh: cannot set terminal process group (-1): Inappropriate ioctl for device sh: no job control in this shellor
/usr/local/share/aliyun-assist/work/script/t-xx.sh: 0: /usr/local/share/aliyun-assist/work/script/t-hz02vmfqb9vxxq8.sh: can't access tty; job control turned off stdin: is not a tty
-
Differences in resource limits
The results of running the ulimit -n command can differ. Cloud Assistant may return a value of 1024, while a local shell returns 65535.
Cause
The /etc/security/limits.conf file in a Linux public image sets resource limits for user login sessions. When you run cat /etc/security/limits.conf locally on an ECS instance, you can see that the maximum number of open file descriptors is typically set to 65535. This is why running ulimit -n in a local session returns 65535.
[root@localhost ~]# cat /etc/security/limits.conf
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
[root@localhost ~]# ulimit -n
65535
When you run the ulimit -n command by using Cloud Assistant, the /etc/security/limits.conf resource limit file has no effect on system services. The Cloud Assistant service process is started by init, which runs as a system service, and is limited to a maximum of 1024 open file descriptors. Therefore, when Cloud Assistant runs ulimit -n, the output is 1024.
You can run the cat /proc/Cloud_Assistant_process_PID/limits command locally on an ECS instance to view the resource limits of the Cloud Assistant service process.
[root@localhost ~]# cat /proc/$(pgrep aliyun-service)/limits
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 8388608 unlimited bytes
Max core file size unlimited unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 30546 30546 processes
Max open files 1024 262144 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 30546 30546 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
Solution
You can increase the resource limit for the Cloud Assistant service to 65535. The method you use depends on the system startup method of your ECS instance.
-
Check the system startup method of your ECS instance.
-
Establish a remote connection to the Linux instance.
For more information, see Connect to an ECS instance.
-
Run the following commands to check the system startup method.
-
systemd
strings $(which init) |grep "systemd"If the output contains "systemd", your system uses the systemd startup method.
-
upstart
strings $(which init) | grep "upstart"If the output contains "upstart", your system uses the upstart startup method.
-
sysvinit
strings $(which init) | grep "sysvinit"If the output contains "sysvinit", your system uses the sysvinit startup method.
-
-
-
Set the resource limit for the Cloud Assistant service.
Follow the instructions for your system's startup method.
Systemd
-
Set resource limits for all services
-
Connect to the Linux instance.
For more information, see Connect to an ECS instance.
-
Edit the /etc/systemd/system.conf file and add the
DefaultLimitNOFILE=65535line under the[Manager]section.vim /etc/systemd/system.conf [Manager] DefaultLimitNOFILE=65535 -
Rerun the systemd manager to apply the changes.
systemctl daemon-reexec -
Restart the Cloud Assistant service.
systemctl restart aliyun.serviceAfter the service restarts, the new resource limit of 65535 is applied.
-
-
Set the resource limit for only the Cloud Assistant service
-
Connect to the Linux instance.
For more information, see Connect to an ECS instance.
-
Edit the configuration file for the Cloud Assistant service and add the
LimitNOFILE=65535line to the[Service]section.[root@localhost ~]# vim /etc/systemd/system/aliyun.service ... [Service] LimitNOFILE=65535 ... -
Reload the systemd configuration.
[root@localhost ~]# systemctl daemon-reload -
Restart the Cloud Assistant service.
[root@localhost ~]# systemctl restart aliyunAfter the service restarts, the new resource limit of 65535 is applied.
-
Non-systemd
-
Modify the Cloud Assistant startup script
-
Connect to the Linux instance.
For more information, see Connect to an ECS instance.
-
Edit the startup script and add
ulimit -n 65535before the command that starts the service process.[root@localhost ~]# vim /etc/init.d/aliyun-service .................. start() { if [ -f $service_lock ]; then ps aux | grep "$start_cmd" | grep -v grep 1>/dev/null 2>&1 if [ $? -eq 0 ]; then echo "$aliyun_service_name is already started!" 1>/dev/null 2>&1 return 0 else rm -rf $service_lock fi fi ulimit -n 65535 $start_cmd ### Create the lock file ### touch $service_lock } .................... -
Restart the Cloud Assistant service.
[root@localhost ~]# /etc/init.d/aliyun-service restartAfter the service restarts, the new resource limit of 65535 is applied.
-
-
Modify the Cloud Assistant configuration file
NoteThis method is supported only on systems that use the upstart startup method.
-
Connect to the Linux instance.
For more information, see Connect to an ECS instance.
-
Edit the Cloud Assistant configuration file and change the
limit nofilevalue to 65535.[root@localhost ~]# vim /etc/init/aliyun-service.conf description "Aliyun Assist Tool" author "aliyun.com" start on runlevel [2345] stop on runlevel [!2345] limit nofile 65535 65535 respawn exec /usr/sbin/aliyun-service post-stop exec sleep 1 -
Restart the Cloud Assistant service.
[root@localhost ~]# service aliyun-service restartAfter the service restarts, the new resource limit of 65535 is applied.
-
-
Differences in output completeness
Command output from Cloud Assistant may be incomplete compared to the output from a command run locally on an ECS instance.
Cause
Cloud Assistant limits the size of command output to 16 KB. If a command generates an output larger than 16 KB, the output will be truncated.
Solution
Redirect large command outputs to a local log file or save them to Object Storage Service (OSS).
Differences in error reporting
A command may fail when run with Cloud Assistant but appear to succeed when run locally on an ECS instance.
Cause
-
Possible cause 1: Exit codes
For both Cloud Assistant and local execution, the exit code of a script is determined by the return value of its last command. If this value is non-zero, the execution is considered a failure. Some commands might produce no visible error messages in the output but still return a non-zero exit code. While you might overlook this during local execution, Cloud Assistant correctly reports the non-zero exit code as a failure.
-
Possible cause 2: Interpreter differences
Cloud Assistant saves a command as a script and then runs it using
sh -c "./script". When you run a command locally, you often use the Bash interpreter. Because the interpreters may differ, a command might fail in Cloud Assistant but work locally. For example, a basicshinterpreter does not support Bash-specific syntax like process substitution (<(cmd)). If your script uses such syntax, it will fail when run by Cloud Assistant.
Solution
To ensure your script runs with the Bash interpreter, add #!/bin/bash to the first line of your command.
Command execution differences on specific operating systems
On Debian, Ubuntu, and UOS instances, a script may run correctly in a local shell but fail with a syntax error when executed through Cloud Assistant.
Cause
When you log on to a Debian, Ubuntu, or UOS instance interactively, the default shell environment is typically Bash (/bin/bash), as defined in /etc/adduser.conf. However, when a script is run in a non-interactive environment without an explicitly defined interpreter, the system defaults to using /bin/sh.
On these operating systems, /bin/sh is linked to dash, a lightweight shell that is not fully compatible with Bash. As a result, if your script uses Bash-specific syntax, such as the source command or the function keyword for defining functions, it will fail due to syntax incompatibility when run by Cloud Assistant. For more information, see the Debian Shell wiki page.
Solution
To ensure your script runs with the Bash interpreter, add #!/bin/bash to the first line of your command.