2020-03-04 12:35:06 +03:00
#!/usr/bin/env bash
2021-10-17 19:07:22 +03:00
# SPDX-License-Identifier: LGPL-2.1-or-later
2021-09-29 21:30:08 +03:00
set -eux
2017-12-14 14:17:43 +03:00
set -o pipefail
# sleep interval (seconds)
2021-09-29 21:30:08 +03:00
sleep_interval = " ${ sleep_interval :- 1 } "
2017-12-14 14:17:43 +03:00
# extend_timeout_interval second(s)
2021-09-29 21:30:08 +03:00
extend_timeout_interval = " ${ extend_timeout_interval :- 1 } "
2017-12-14 14:17:43 +03:00
# number of sleep_intervals before READY=1
2021-09-29 21:30:08 +03:00
start_intervals = " ${ start_intervals :- 10 } "
2017-12-14 14:17:43 +03:00
# number of sleep_intervals before exiting
2021-09-29 21:30:08 +03:00
stop_intervals = " ${ stop_intervals :- 10 } "
2017-12-14 14:17:43 +03:00
# run intervals, number of sleep_intervals to run
2021-09-29 21:30:08 +03:00
run_intervals = " ${ run_intervals :- 7 } "
2017-12-14 14:17:43 +03:00
# We convert to usec
2021-09-29 21:30:08 +03:00
extend_timeout_interval = $(( extend_timeout_interval * 1000000 ))
2017-12-14 14:17:43 +03:00
2021-09-29 21:30:08 +03:00
# shellcheck disable=SC2064
trap " { touch / ${ SERVICE } .terminated; exit 1; } " SIGTERM SIGABRT
2017-12-14 14:17:43 +03:00
2021-09-29 21:30:08 +03:00
rm -f " / ${ SERVICE } " .*
touch " / ${ SERVICE } .startfail "
2017-12-14 14:17:43 +03:00
2021-09-29 21:30:08 +03:00
systemd-notify EXTEND_TIMEOUT_USEC = " $extend_timeout_interval "
while [ [ $start_intervals -gt 0 ] ]
2017-12-14 14:17:43 +03:00
do
2021-09-29 21:30:08 +03:00
sleep " $sleep_interval "
start_intervals = $(( start_intervals - 1 ))
systemd-notify EXTEND_TIMEOUT_USEC = " $extend_timeout_interval "
2017-12-14 14:17:43 +03:00
done
systemd-notify --ready --status= "Waiting for your request"
2021-09-29 21:30:08 +03:00
touch " / ${ SERVICE } .runtimefail "
rm " / ${ SERVICE } .startfail "
2017-12-14 14:17:43 +03:00
2021-09-29 21:30:08 +03:00
systemd-notify EXTEND_TIMEOUT_USEC = " $extend_timeout_interval "
while [ [ $run_intervals -gt 0 ] ]
2017-12-14 14:17:43 +03:00
do
2021-09-29 21:30:08 +03:00
sleep " $sleep_interval "
run_intervals = $(( run_intervals - 1 ))
systemd-notify EXTEND_TIMEOUT_USEC = " $extend_timeout_interval "
2017-12-14 14:17:43 +03:00
done
systemd-notify STOPPING = 1
2021-09-29 21:30:08 +03:00
touch " / ${ SERVICE } .stopfail "
rm " / ${ SERVICE } .runtimefail "
2017-12-14 14:17:43 +03:00
2021-09-29 21:30:08 +03:00
systemd-notify EXTEND_TIMEOUT_USEC = " $extend_timeout_interval "
while [ [ $stop_intervals -gt 0 ] ]
2017-12-14 14:17:43 +03:00
do
2021-09-29 21:30:08 +03:00
sleep " $sleep_interval "
stop_intervals = $(( stop_intervals - 1 ))
systemd-notify EXTEND_TIMEOUT_USEC = " $extend_timeout_interval "
2017-12-14 14:17:43 +03:00
done
2021-09-29 21:30:08 +03:00
touch " / ${ SERVICE } .success "
rm " / ${ SERVICE } .stopfail "
2017-12-14 14:17:43 +03:00
exit 0