Force Hero
Force Hero

Reputation: 3202

My shell script does not see environment variable changes

I have a bash script which loops forever and inside it checks an environment variable to see if it should run php yii process-queue or not. This script is the command for a docker container by the way and so is PID 1.

Output from 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

When I run export RUNPROCQ=true from the command line I expected the loop to start running php yii process-queue but it doesn't - is there a command I can run in my bash script so it can see the RUNPROCQ environment variable value change?

My bash script called process-queue-runner.sh:

#!/bin/bash

while :
do
    if [[ ${RUNPROCQ} != "false" ]]; then
        php yii process-queue
    fi
    sleep 0.5
done

Here is the relevant section from the docker-compose.yml file:

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

Upvotes: 0

Views: 1949

Answers (3)

Claudiu
Claudiu

Reputation: 99

It's not gonna work like that because you are setting the RUNPROCQ variable in the parent shell and your script will not be able to read it. Maybe you should try something like this in process-queue-runner.sh:

#!/bin/bash
while :
do
    source /tmp/.myvars
    if [[ ${RUNPROCQ} != "false" ]]; then
        php yii process-queue
    fi
    sleep 0.5
done

In refresh_db.sh add:

RUNPROCQ="true"
#or
RUNPROCQ="false"
#...
echo RUNPROCQ="$RUNPROCQ" > /tmp/.myvars

Upvotes: 1

Meiram Chuzhenbayev
Meiram Chuzhenbayev

Reputation: 906

user supervisord and forget about this manual script. supervisor can grab all logs and monitoring the processes

supervisord

Upvotes: 0

ceving
ceving

Reputation: 23764

Your script is fine.

$ export RUNPROCQ=true
$ ./ba.sh
php yii process-queue
php yii process-queue
php yii process-queue
php yii process-queue
^C
$ cat ba.sh 
while :
do
    if [[ ${RUNPROCQ} != "false" ]]; then
        echo php yii process-queue
    fi
    sleep 0.5
done

Upvotes: 0

Related Questions