entrypoint.sh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env bash
  2. #
  3. # usage: entrypoint.sh timeout command_by_base64 command_args
  4. # timeout: command execute timeout
  5. # command_by_base64: command encode by base64
  6. # command_args: position arguments for command
  7. set -e
  8. trap "[ -z $1 ] && sleep 777d" EXIT
  9. if [ -z $1 ]; then
  10. bash -c "$(echo ${__DEPLOY_START} | base64 -d)" && echo v_start exited || echo v_start exited, exit code $?
  11. elif [ $1 == 'kill' ]; then
  12. # $1 string for 'kill'
  13. # $2 exec token
  14. if [ -f /tmp/$2 ]; then
  15. main_pid=$(head -1 /tmp/$2 | cut -c 2-)
  16. pkill -9 -P ${main_pid} || echo -n
  17. rm -f /tmp/$2
  18. fi
  19. elif [ $1 == 'wait' ]; then
  20. # $1 string for 'wait'
  21. # $2 timeout seconds
  22. # $3 exec token
  23. # $4 main process pid
  24. for ((COUNTER=0; COUNTER < $2; ++COUNTER)); do
  25. #if ! ps -p $4 &> /dev/null; then
  26. if ! ps |awk '{print $1}'|grep -E "^$4$" &> /dev/null; then
  27. break
  28. fi
  29. sleep 1
  30. done
  31. rm -f /tmp/$3
  32. pkill -9 -P $4
  33. else
  34. # $1 timeout seconds
  35. # $2 exec token
  36. # $3 exec content for base64 encode
  37. temp_file=/tmp/$2
  38. nohup /entrypoint.sh wait $1 $2 $$ &
  39. echo "#$$" > ${temp_file}
  40. echo $3 | base64 -d >> ${temp_file}
  41. shift
  42. shift
  43. shift
  44. source ${temp_file}
  45. fi