【发布时间】:2017-09-08 00:32:53
【问题描述】:
我有一个 bash 脚本,它永远循环并在其中检查环境变量以查看它是否应该运行 php yii process-queue。顺便说一下,这个脚本是 docker 容器的命令,PID 1 也是。
来自ps aux的输出:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 19720 3340 ? Ss 09:26 0:00 /bin/bash /var/www/hub/process-queue-runner.sh
root 293 0.0 0.0 19936 3664 ? Ss 09:28 0:00 /bin/bash
root 1927 0.0 0.0 6012 648 ? S 09:42 0:00 sleep 0.5
root 1928 0.0 0.0 36092 3164 ? R+ 09:42 0:00 ps aux
当我从命令行运行 export RUNPROCQ=true 时,我希望循环开始运行 php yii process-queue 但它没有 - 有没有可以在我的 bash 脚本中运行的命令,以便它可以看到 RUNPROCQ 环境变量值变化?
我的 bash 脚本名为 process-queue-runner.sh:
#!/bin/bash
while :
do
if [[ ${RUNPROCQ} != "false" ]]; then
php yii process-queue
fi
sleep 0.5
done
这是 docker-compose.yml 文件中的相关部分:
procq:
image: hub_ui:latest
environment:
ENV: qa1
RUNPROCQ: "false" # this is to stop the proc q from running straight away - the refresh_db.sh script will set this to true once it has finished loading the fixtures
links:
- db:local.database.hub
- cache:local.cache.hub
command: /var/www/hub/process-queue-runner.sh
【问题讨论】:
-
你是如何执行脚本的?
-
@Inian 这是 docker 容器的命令 - 我已将相关的 docker-compose.yml 部分添加到我的问题中。
-
问题在于在子 shell 中运行的脚本的 执行,没有反映您对环境变量的更改。也许认为您可以source它?
-
我设法让它像这样运行:
root 1 0.0 0.0 19720 3272 ? Ss 10:12 0:00 /bin/bash -c source /var/www/hub/process-queue-runner.sh但是更改 env 变量值仍然没有任何作用...... -
我最终只是创建和删除了一个隐藏文件并测试了该文件是否存在。不理想,但它适用于较小的磁盘 i/o 开销。
标签: linux shell environment-variables