miniond 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/bash
  2. #
  3. # This script is similar to minion but will do a few additional things:
  4. # - PHP process is run in the background
  5. # - PHP process is monitored and restarted if it exits for any reason
  6. # - Added handlers for SUGHUP, SIGINT, and SIGTERM
  7. #
  8. # This is meant for long running minion tasks (like background workers).
  9. # Shutting down the minion tasks is done by sending a SIGINT or SIGTERM signal
  10. # to this miniond process. You can also restart the minion task by sending a
  11. # SUGHUP signal to this process. It's useful to restart all your workers when
  12. # deploying new code so that the workers reload their code as well.
  13. # You cannot use this script for tasks that require user input because of the
  14. # PHP process running in the background.
  15. #
  16. # Usage: ./miniond [task:name] [--option1=optval1 --option2=optval2]
  17. #
  18. # And so on.
  19. #
  20. # Define some functions
  21. function start_daemon()
  22. {
  23. echo "Starting"
  24. ./minion $ARGS & # This assumes miniond is sitting next to minion
  25. pid=$! # Store pid (globally) for later use..
  26. }
  27. function stop_daemon()
  28. {
  29. kill -TERM $pid
  30. wait $pid # Wait for the task to exit and store the exit code
  31. ecode=$? # Store exit code (globally) for later use..
  32. }
  33. function handle_SIGHUP()
  34. {
  35. echo "Restarting"
  36. stop_daemon
  37. start_daemon
  38. }
  39. function handle_SIGTERM_SIGINT()
  40. {
  41. echo "Shutting Down"
  42. stop_daemon
  43. exit $ecode
  44. }
  45. # Register signal handlers
  46. trap handle_SIGHUP SIGHUP
  47. trap handle_SIGTERM_SIGINT SIGTERM SIGINT
  48. ARGS=$@
  49. start_daemon
  50. while :
  51. do
  52. # Pauses the script until $pid is dead
  53. wait $pid
  54. # Make sure someone didn't start it back up already (SIGHUP handler does this)
  55. if ! kill -0 $pid > /dev/null 2>&1
  56. then
  57. start_daemon
  58. fi
  59. done