| @@ -4,6 +4,8 @@ I’m coming across having to write the same shell script bits occasionally, and | |||||
| This is a *slowly* growing collection of hopefully useful and reusable shell functions and patterns. | This is a *slowly* growing collection of hopefully useful and reusable shell functions and patterns. | ||||
| Currently tested in macOS only. | |||||
| Copyright (c) 2022 Jan Lehnardt <jan@apache.org>, MIT licensed | Copyright (c) 2022 Jan Lehnardt <jan@apache.org>, MIT licensed | ||||
| ## Goals | ## Goals | ||||
| @@ -29,8 +31,6 @@ This section documents reusable functions for your scripts. | |||||
| Wait for a particular URL to be available using `curl`. Wait `$duration` seconds between attempts, try `$iterations` times. Defaults to `1` seconds and `3` times. | Wait for a particular URL to be available using `curl`. Wait `$duration` seconds between attempts, try `$iterations` times. Defaults to `1` seconds and `3` times. | ||||
| Requires `bash`. | |||||
| Signature: | Signature: | ||||
| ```shell | ```shell | ||||
| @@ -56,8 +56,6 @@ wait_for_url http://127.0.0.1:5984 5 3 | |||||
| Wait for a particular PID to exit. Wait `$duration` seconds between attempts, try `$iterations` times. Defaults to `1` seconds and `3` times. | Wait for a particular PID to exit. Wait `$duration` seconds between attempts, try `$iterations` times. Defaults to `1` seconds and `3` times. | ||||
| Requires `bash`. | |||||
| Signature: | Signature: | ||||
| ```shell | ```shell | ||||
| @@ -14,12 +14,12 @@ wait_for_pid_exit () { | |||||
| debug_inline "waiting for $pid to exit " | debug_inline "waiting for $pid to exit " | ||||
| until ( ! ps ax | grep $pid | grep -v grep > /dev/null); do | until ( ! ps ax | grep $pid | grep -v grep > /dev/null); do | ||||
| if [ $waiting -eq $iterations ]; then | if [ $waiting -eq $iterations ]; then | ||||
| let after=$duration*iterations | |||||
| after=`expr $duration \* $iterations` | |||||
| debug_inline "failed, $pid still running after $after seconds\n" | debug_inline "failed, $pid still running after $after seconds\n" | ||||
| return 1 | return 1 | ||||
| fi | fi | ||||
| debug_inline . | debug_inline . | ||||
| let waiting=waiting+1 | |||||
| waiting=`expr $waiting + 1` | |||||
| sleep $duration | sleep $duration | ||||
| done | done | ||||
| debug_inline " succeeded\n" | debug_inline " succeeded\n" | ||||
| @@ -17,12 +17,12 @@ wait_for_url () { | |||||
| debug_inline "waiting for $url " | debug_inline "waiting for $url " | ||||
| until ($cmd); do | until ($cmd); do | ||||
| if [ $waiting -eq $iterations ]; then | if [ $waiting -eq $iterations ]; then | ||||
| let after=$duration*iterations | |||||
| after=`expr $duration \* $iterations` | |||||
| debug_inline "failed, $url can not be reached after $after seconds\n" | debug_inline "failed, $url can not be reached after $after seconds\n" | ||||
| return 1 | return 1 | ||||
| fi | fi | ||||
| debug_inline . | debug_inline . | ||||
| let waiting=waiting+1 | |||||
| waiting=`expr $waiting + 1` | |||||
| sleep $duration | sleep $duration | ||||
| done | done | ||||
| debug_inline " succeeded\n" | debug_inline " succeeded\n" | ||||