From: Matthias Kruk Date: Sun, 13 Jun 2021 08:12:48 +0000 (+0900) Subject: include/inst: Add functions for implementing singleton daemons X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=f946a8bdffcbca6ee5a4bf657603a0e6334e4e33;p=toolbox include/inst: Add functions for implementing singleton daemons The inst_start() function does not check if a process is already running, making it unusable for the implementation of singleton daemons. This commit adds the inst_singleton() function, which allows the caller to implement daemons which can only be run one at a time. --- diff --git a/include/inst.sh b/include/inst.sh index 9e697c9..678a2ae 100644 --- a/include/inst.sh +++ b/include/inst.sh @@ -233,3 +233,29 @@ inst_get_status_timestamp() { echo "${status%%:*}" return 0 } + +inst_count() { + local -i num + + if ! num=$(find "$__inst_path" -regex ".*/[0-9]+" | wc -l); then + return 1 + fi + + echo "$num" + return 0 +} + +inst_singleton() { + local args=("$@") + + if (( $(inst_count) > 0 )); then + log_error "Another instance is already running" + return 1 + fi + + if ! inst_start "${args[@]}"; then + return 1 + fi + + return 0 +}