0

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
5
  • How did you execute the script? Commented Apr 12, 2017 at 9:44
  • @Inian It's the command for a docker container - I've added the relevant docker-compose.yml section to my question. Commented Apr 12, 2017 at 9:49
  • The problem is with the execution of the script which runs in a sub-shell, not reflecting your changes in the environment variables. Perhaps think you can source it? Commented Apr 12, 2017 at 9:51
  • I managed to get it running like this: root 1 0.0 0.0 19720 3272 ? Ss 10:12 0:00 /bin/bash -c source /var/www/hub/process-queue-runner.sh But changing the env variable value still doesn't do anything... Commented Apr 12, 2017 at 10:28
  • I ended up just creating and deleting a hidden file and testing for the presence of that file in the end. Not ideal but it works with a small disk i/o overhead. Commented Apr 12, 2017 at 12:09

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! You're solution is more elegant than what I went with although it solves the problem in the same manner - use the disk as a common place to read/write stuff. I just used the presence of a hidden file to determine whether to run the command or not. Appreciate your feedback.
0

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

Comments

0

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

supervisord

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.