diff --git a/README.md b/README.md
index 0c49e43..f780379 100755
--- a/README.md
+++ b/README.md
@@ -114,6 +114,7 @@ This repo contains two types of scripts, posix compatible and bash compatible.
| sed | Miscellaneous |
| mktemp | To generate temporary files ( optional ) |
| sleep | Self explanatory |
+| ps | To manage different processes |
If BASH is not available or BASH is available but version is less tham 4.x, then below programs are also required:
@@ -124,11 +125,10 @@ This repo contains two types of scripts, posix compatible and bash compatible.
| cat | Miscellaneous |
| stty or zsh or tput | To determine column size ( optional ) |
-These programs are needed for synchronisation script:
+These are the additional programs needed for synchronisation script:
| Program | Role In Script |
| ------------- | ------------------------- |
-| ps | To manage background jobs |
| tail | To show indefinite logs |
### Installation
diff --git a/bash/release/gupload b/bash/release/gupload
index 40a9d5f..386709f 100755
--- a/bash/release/gupload
+++ b/bash/release/gupload
@@ -1293,7 +1293,30 @@ _check_credentials() {
fi
}
- [[ -z ${ACCESS_TOKEN} || ${ACCESS_TOKEN_EXPIRY} -lt "$(printf "%(%s)T\\n" "-1")" ]] && { _get_access_token_and_update || return 1; }
+ [[ -z ${ACCESS_TOKEN} || ${ACCESS_TOKEN_EXPIRY:-0} -lt "$(printf "%(%s)T\\n" "-1")" ]] && { _get_access_token_and_update || return 1; }
+
+ # launch a background service to check access token and update it
+ # checks ACCESS_TOKEN_EXPIRY, try to update before 5 mins of expiry, a fresh token gets 60 mins
+ # process will be killed when script exits
+ {
+ while :; do
+ CURRENT_TIME="$(printf "%(%s)T\\n" "-1")"
+ REMAINING_TOKEN_TIME="$((ACCESS_TOKEN_EXPIRY - CURRENT_TIME))"
+ if [[ ${REMAINING_TOKEN_TIME} -le 300 ]]; then
+ # timeout after 30 seconds, it shouldn't take too long anyway
+ _timeout 30 _get_access_token_and_update || :
+ else
+ TOKEN_PROCESS_TIME_TO_SLEEP="$(if [[ ${REMAINING_TOKEN_TIME} -le 301 ]]; then
+ printf "0\n"
+ else
+ printf "%s\n" "$((REMAINING_TOKEN_TIME - 300))"
+ fi)"
+ sleep "${TOKEN_PROCESS_TIME_TO_SLEEP}"
+ fi
+ sleep 1
+ done
+ } &
+ ACCESS_TOKEN_SERVICE_PID="${!}"
return 0
}
@@ -1566,6 +1589,16 @@ main() {
_cleanup() {
{
[[ -n ${PARALLEL_UPLOAD} ]] && rm -f "${TMPFILE:?}"*
+
+ # grab all chidren processes of access token service
+ # https://askubuntu.com/a/512872
+ token_service_pids="$(ps --ppid="${ACCESS_TOKEN_SERVICE_PID}" -o pid=)"
+ # first kill parent id, then children processes
+ kill "${ACCESS_TOKEN_SERVICE_PID}"
+ for pid in ${token_service_pids}; do
+ kill "${pid}"
+ done
+
export abnormal_exit && if [[ -n ${abnormal_exit} ]]; then
printf "\n\n%s\n" "Script exited manually."
kill -- -$$ &
diff --git a/bash/upload.bash b/bash/upload.bash
index 4e62821..ee574cb 100755
--- a/bash/upload.bash
+++ b/bash/upload.bash
@@ -367,7 +367,30 @@ _check_credentials() {
fi
}
- [[ -z ${ACCESS_TOKEN} || ${ACCESS_TOKEN_EXPIRY} -lt "$(printf "%(%s)T\\n" "-1")" ]] && { _get_access_token_and_update || return 1; }
+ [[ -z ${ACCESS_TOKEN} || ${ACCESS_TOKEN_EXPIRY:-0} -lt "$(printf "%(%s)T\\n" "-1")" ]] && { _get_access_token_and_update || return 1; }
+
+ # launch a background service to check access token and update it
+ # checks ACCESS_TOKEN_EXPIRY, try to update before 5 mins of expiry, a fresh token gets 60 mins
+ # process will be killed when script exits
+ {
+ while :; do
+ CURRENT_TIME="$(printf "%(%s)T\\n" "-1")"
+ REMAINING_TOKEN_TIME="$((ACCESS_TOKEN_EXPIRY - CURRENT_TIME))"
+ if [[ ${REMAINING_TOKEN_TIME} -le 300 ]]; then
+ # timeout after 30 seconds, it shouldn't take too long anyway
+ _timeout 30 _get_access_token_and_update || :
+ else
+ TOKEN_PROCESS_TIME_TO_SLEEP="$(if [[ ${REMAINING_TOKEN_TIME} -le 301 ]]; then
+ printf "0\n"
+ else
+ printf "%s\n" "$((REMAINING_TOKEN_TIME - 300))"
+ fi)"
+ sleep "${TOKEN_PROCESS_TIME_TO_SLEEP}"
+ fi
+ sleep 1
+ done
+ } &
+ ACCESS_TOKEN_SERVICE_PID="${!}"
return 0
}
@@ -640,6 +663,16 @@ main() {
_cleanup() {
{
[[ -n ${PARALLEL_UPLOAD} ]] && rm -f "${TMPFILE:?}"*
+
+ # grab all chidren processes of access token service
+ # https://askubuntu.com/a/512872
+ token_service_pids="$(ps --ppid="${ACCESS_TOKEN_SERVICE_PID}" -o pid=)"
+ # first kill parent id, then children processes
+ kill "${ACCESS_TOKEN_SERVICE_PID}"
+ for pid in ${token_service_pids}; do
+ kill "${pid}"
+ done
+
export abnormal_exit && if [[ -n ${abnormal_exit} ]]; then
printf "\n\n%s\n" "Script exited manually."
kill -- -$$ &
diff --git a/sh/release/gupload b/sh/release/gupload
index b916df8..a3e2ddb 100755
--- a/sh/release/gupload
+++ b/sh/release/gupload
@@ -1286,7 +1286,30 @@ _check_credentials() {
fi
}
- [ -z "${ACCESS_TOKEN}" ] || [ "${ACCESS_TOKEN_EXPIRY}" -lt "$(date +'%s')" ] && { _get_access_token_and_update || return 1; }
+ [ -z "${ACCESS_TOKEN}" ] || [ "${ACCESS_TOKEN_EXPIRY:-0}" -lt "$(date +'%s')" ] && { _get_access_token_and_update || return 1; }
+
+ # launch a background service to check access token and update it
+ # checks ACCESS_TOKEN_EXPIRY, try to update before 5 mins of expiry, a fresh token gets 60 mins
+ # process will be killed when script exits
+ {
+ while :; do
+ CURRENT_TIME="$(date +'%s')"
+ REMAINING_TOKEN_TIME="$((CURRENT_TIME - ACCESS_TOKEN_EXPIRY))"
+ if [ "${REMAINING_TOKEN_TIME}" -le 300 ]; then
+ # timeout after 30 seconds, it shouldn't take too long anyway
+ _timeout 30 _get_access_token_and_update || :
+ else
+ TOKEN_PROCESS_TIME_TO_SLEEP="$(if [ "${REMAINING_TOKEN_TIME}" -le 301 ]; then
+ printf "0\n"
+ else
+ printf "%s\n" "$((REMAINING_TOKEN_TIME - 300))"
+ fi)"
+ sleep "${TOKEN_PROCESS_TIME_TO_SLEEP}"
+ fi
+ sleep 1
+ done
+ } &
+ ACCESS_TOKEN_SERVICE_PID="${!}"
return 0
}
@@ -1575,6 +1598,16 @@ main() {
_cleanup() {
{
[ -n "${PARALLEL_UPLOAD}" ] && rm -f "${TMPFILE:?}"*
+
+ # grab all chidren processes of access token service
+ # https://askubuntu.com/a/512872
+ token_service_pids="$(ps --ppid="${ACCESS_TOKEN_SERVICE_PID}" -o pid=)"
+ # first kill parent id, then children processes
+ kill "${ACCESS_TOKEN_SERVICE_PID}"
+ for pid in ${token_service_pids}; do
+ kill "${pid}"
+ done
+
export abnormal_exit && if [ -n "${abnormal_exit}" ]; then
printf "\n\n%s\n" "Script exited manually."
kill -9 -$$ &
diff --git a/sh/upload.sh b/sh/upload.sh
index c059c6f..72394bf 100755
--- a/sh/upload.sh
+++ b/sh/upload.sh
@@ -377,7 +377,30 @@ _check_credentials() {
fi
}
- [ -z "${ACCESS_TOKEN}" ] || [ "${ACCESS_TOKEN_EXPIRY}" -lt "$(date +'%s')" ] && { _get_access_token_and_update || return 1; }
+ [ -z "${ACCESS_TOKEN}" ] || [ "${ACCESS_TOKEN_EXPIRY:-0}" -lt "$(date +'%s')" ] && { _get_access_token_and_update || return 1; }
+
+ # launch a background service to check access token and update it
+ # checks ACCESS_TOKEN_EXPIRY, try to update before 5 mins of expiry, a fresh token gets 60 mins
+ # process will be killed when script exits
+ {
+ while :; do
+ CURRENT_TIME="$(date +'%s')"
+ REMAINING_TOKEN_TIME="$((CURRENT_TIME - ACCESS_TOKEN_EXPIRY))"
+ if [ "${REMAINING_TOKEN_TIME}" -le 300 ]; then
+ # timeout after 30 seconds, it shouldn't take too long anyway
+ _timeout 30 _get_access_token_and_update || :
+ else
+ TOKEN_PROCESS_TIME_TO_SLEEP="$(if [ "${REMAINING_TOKEN_TIME}" -le 301 ]; then
+ printf "0\n"
+ else
+ printf "%s\n" "$((REMAINING_TOKEN_TIME - 300))"
+ fi)"
+ sleep "${TOKEN_PROCESS_TIME_TO_SLEEP}"
+ fi
+ sleep 1
+ done
+ } &
+ ACCESS_TOKEN_SERVICE_PID="${!}"
return 0
}
@@ -666,6 +689,16 @@ main() {
_cleanup() {
{
[ -n "${PARALLEL_UPLOAD}" ] && rm -f "${TMPFILE:?}"*
+
+ # grab all chidren processes of access token service
+ # https://askubuntu.com/a/512872
+ token_service_pids="$(ps --ppid="${ACCESS_TOKEN_SERVICE_PID}" -o pid=)"
+ # first kill parent id, then children processes
+ kill "${ACCESS_TOKEN_SERVICE_PID}"
+ for pid in ${token_service_pids}; do
+ kill "${pid}"
+ done
+
export abnormal_exit && if [ -n "${abnormal_exit}" ]; then
printf "\n\n%s\n" "Script exited manually."
kill -9 -$$ &