You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Elastic Beanstalk钩子执行失败:exec format error问题求助

AWS Elastic Beanstalk钩子脚本exec format error问题解决

问题场景

我在AWS Elastic Beanstalk中配置了predeploy/postdeploy钩子,文件结构如下:

+-- .platform
    +-- hooks
        +-- predeploy
            +-- 01_npm_install_and_build.sh

脚本内容为:

curl --silent --location https://rpm.nodesource.com/setup_16.x | sudo bash -
sudo yum -y install nodejs
cd /var/app/current/
sudo npm install
sudo npm run build

通过SSH连接实例后用sh 01_npm_install_and_build.sh执行脚本完全正常,但查看/var/log/eb-engine.log时发现钩子执行失败,报错信息如下:

[ERROR] An error occurred during execution of command [app-deploy] -
[RunAppDeployPostDeployHooks]. Stop running the command. Error:
Command .platform/hooks/postdeploy/01_npm_install_and_build.sh failed
with error fork/exec
.platform/hooks/postdeploy/01_npm_install_and_build.sh: exec format
error

问题原因

出现exec format error主要有两个常见原因:

  • 脚本没有指定解释器(Shebang):Elastic Beanstalk执行钩子时会直接运行脚本,而非通过sh调用,必须明确声明解释器路径。
  • 脚本使用DOS换行格式(CRLF):Linux系统无法识别Windows风格的换行符,会导致执行时格式错误。

解决步骤

1. 给脚本添加Shebang

在脚本的第一行添加#!/bin/bash,指定bash作为执行解释器:

#!/bin/bash
curl --silent --location https://rpm.nodesource.com/setup_16.x | sudo bash -
sudo yum -y install nodejs
cd /var/app/current/
sudo npm install
sudo npm run build

2. 转换脚本为Unix换行格式

如果是在Windows环境编写的脚本,需要将换行符从CRLF改为LF:

  • VS Code:点击右下角的CRLF选项,切换为LF
  • Notepad++:依次点击「编辑」→「文档格式转换」→「转换为UNIX格式」
  • 也可以在Linux实例上执行命令转换:
    sed -i 's/\r$//' 01_npm_install_and_build.sh
    

3. 确保脚本有可执行权限(可选)

虽然Elastic Beanstalk一般会自动处理权限,但手动设置可执行权限能避免潜在问题:

chmod +x 01_npm_install_and_build.sh

内容的提问来源于stack exchange,提问作者Jonathan Clark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 00:15:49