This is a slowly growing collection of hopefully useful and reusable shell functions and patterns. https://code.jan.io/jan/jans_shell_utils
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wait_for_pid_exit.sh 996B

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
12345678910111213141516171819202122232425262728293031
  1. # Copyright (c) 2022 Jan Lehnardt <jan@apache.org>, MIT licensed
  2. . ./lib.sh
  3. wait_for_pid_exit () {
  4. assert_arg "$1" && pid="$1" || error_and_exit "pid not provided, exiting"
  5. assert_arg "$2" && iterations="$2" || iterations=3
  6. assert_arg "$3" && duration="$3" || duration=1
  7. debug "called wait_for_url(pid=pid, duration=$duration, iterations=$iterations)"
  8. waiting=0
  9. debug_inline "waiting for $pid to exit "
  10. # show all pids
  11. # | find just the $server_pid
  12. # | | don’t match on the previous grep
  13. # | | | we don’t need the output
  14. # | | | |
  15. until ( ! ps ax | grep $pid | grep -v grep > /dev/null); do
  16. if [ $waiting -eq $iterations ]; then
  17. after=`expr $duration \* $iterations`
  18. debug_inline "failed, $pid still running after $after seconds\n"
  19. return 1
  20. fi
  21. debug_inline .
  22. waiting=`expr $waiting + 1`
  23. sleep $duration
  24. done
  25. debug_inline " succeeded\n"
  26. }