在我的应用程序部署到 AWS Elasticbeanstalk 后,我尝试运行 celery Worker。
99_celery_start.sh
#!/usr/bin/env bash
#make dirs
sudo mkdir -p /usr/etc/
sudo chmod 755 /usr/etc/
sudo touch /usr/etc/celery.conf
sudo touch /usr/etc/supervisord.conf
# Get django environment variables
celeryenv=`cat /var/app/rootfolder/myprject/.env | tr '\n' ',' | sed 's/export //g' | sed 's/$PATH/%(ENV_PATH)s/g' | sed 's/$PYTHONPATH//g' | sed 's/$LD_LIBRARY_PATH//g' | sed 's/%/%%/g'`
celeryenv=${celeryenv%?}
# Create celery configuration script
celeryconf="[program:celery-worker]
user=root
directory=/var/app/rootfolder
; Set full path to celery program if using virtualenv
command=/var/app/venv/*/bin/celery -A myprject worker -P solo --loglevel=INFO
.
.
.
.
celery.config
container_commands:
01_celery_configure:
command: "mkdir -p /.platform/hooks/postdeploy/ && cp .ebextensions/99_celery_start.sh /.platform/hooks/postdeploy/ && chmod 774 /.platform/hooks/postdeploy/99_celery_start.sh"
02_run_celery:
command: "sudo /.platform/hooks/postdeploy/99_celery_start.sh"
所以我尝试的是复制 .ebextension 文件夹中的 celery-worker 脚本并将其粘贴到 post-deploy hook 文件夹中,以便该脚本在应用程序部署到实例上后运行。但是命令 02_run_celery 正在执行在提取应用程序并将其部署到实例上之前。由于脚本需要应用程序文件夹 /var/app/rootfolder/myprjct/.env,因此部署过程会给出错误 cat: /var/app/rootfolder/myprjct/.env: No such file or directory。
如果您的假设是正确的并且这是一个竞争条件(第二个命令在应用程序部署之前执行) - 那么等待应用程序部署怎么样?或者等待您感兴趣的特定文件?
...
# Wait for the app deployment
while [ ! -f /var/app/rootfolder/myprject/.env ]
do
sleep 1
echo "Waiting for the application deployment"
done
# Get django environment variables
celeryenv=`cat /var/app/rootfolder/myprject/.env | tr '\n' ',' | sed 's/export //g' | sed 's/$PATH/%(ENV_PATH)s/g' | sed 's/$PYTHONPATH//g' | sed 's/$LD_LIBRARY_PATH//g' | sed 's/%/%%/g'`
celeryenv=${celeryenv%?}
...
如果有人仍在寻找答案,那么我就是这样做的:
文件夹结构:
|-- .ebextensions/
| |-- celery.config # Option settings
| `-- cloudwatch.config # Other .ebextensions sections, for example files and container commands
`-- .platform/
|-- nginx/ # Proxy configuration
| |-- nginx.conf
| `-- conf.d/
| `-- custom.conf
|-- hooks/ # Application deployment hooks
| `-- postdeploy/
| `-- 99_celery_start.sh
现在在 celery.config 中添加 99_celery_start.script 的权限:
01_celery_perm:
command: "sudo chmod +x .platform/hooks/postdeploy/99_celery_start.sh"
02_dos2unix:
command: "perl -i -pe's/\r$//;' .platform/hooks/postdeploy/99_celery_start.sh"
重要提示: 确保脚本应以 LF 行结尾而不是 CRLF 保存。
我遇到了同样的问题 exec format error 因为我正在从 Windows 部署到 amazon linux 。我通过添加解决了这个问题
#!/bin/bash
到 filename.sh 文件的顶部并添加
04_dos2unix:
command: "dos2unix .platform/hooks/postdeploy/filname.sh"
cwd: /var/app/current/
使用容器命令