7

Take for example the following .config file in .ebextensions/

container_commands: 
  000_run_queue_daemon: 
    command: "nohup php artisan queue:work --daemon &"
    test: "ps -ef | grep artisan | grep -v grep > /dev/null || echo 1"

If the daemon is not already running, start a queue worker. The queue worker daemon runs forever (by design) and therefore needs to be run as a background process.

The ampersand seems to have no effect and tailing cfn-init.log just halts at

2014-09-15 00:24:53,921 [DEBUG] Running test for command 000_run_queue_daemon
2014-09-15 00:24:53,929 [DEBUG] Test command output: 1

2014-09-15 00:24:53,929 [DEBUG] Test for command 000_run_queue_daemon passed

This then stays as such until the EB process times out and it gives up with the deployment.

How can I make this run as a background process?

Ben Swinburne
  • 337
  • 4
  • 17

1 Answers1

8

In order to make this work I had to run the command from a file using the post deployment hooks

commands:
  create_post_dir:
    command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
    ignoreErrors: true

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_workers.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      nohup php /var/app/current/artisan queue:work --daemon >/dev/null 2>&1 &
Ben Swinburne
  • 337
  • 4
  • 17
  • Hi, looks like you have solved your own case. Can you mark this answer as accepted? – masegaloeh Feb 18 '15 at 11:08
  • This worked beautifully for me. – user2957009 Mar 21 '19 at 02:55
  • I'm having trouble with this. It seems like the hook works and runs `queue:work` but it does not use the environment variables set in the EB configuration page. The result is that I get the error `Predis\Connection\ConnectionException` with the message `Connection refused [tcp://127.0.0.1:6379]`, since it is using the default values. Googling seems to indicate that the environment variables are not available in hooks. – Marius Jan 31 '20 at 09:05
  • @Marius there are a few ways you can do this. Off the top of my head i think you can just put `source /opt/elasticbeanstalk/support/envvars` into your restart_workers_99.sh file at the top and it'll load the env vars in. Another option which I believe works (again if i remember correctly) is change commands: to container_commands: as container commands run with the env vars set – Ben Swinburne Jan 31 '20 at 22:40
  • This may have also changed on newer versions of elastic beanstalk that use amazon linux 2 – Ben Swinburne Jan 31 '20 at 22:40
  • `sudo /opt/elasticbeanstalk/bin/get-config environment --output YAML|JSON|??` may help you too – Ben Swinburne Jan 31 '20 at 22:42
  • What you can do is add your environment variables in your file e.g. `export ACCESS_KEY_ID_S3_STATIC="XXXX"` above the nohup command. – Josh Mar 17 '20 at 11:45