1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3set -eux
4set -o pipefail
5
6# sleep interval (seconds)
7sleep_interval="${sleep_interval:-1}"
8# extend_timeout_interval second(s)
9extend_timeout_interval="${extend_timeout_interval:-1}"
10# number of sleep_intervals before READY=1
11start_intervals="${start_intervals:-10}"
12# number of sleep_intervals before exiting
13stop_intervals="${stop_intervals:-10}"
14# run intervals, number of sleep_intervals to run
15run_intervals="${run_intervals:-7}"
16
17# We convert to usec
18extend_timeout_interval=$((extend_timeout_interval * 1000000))
19
20# shellcheck disable=SC2064
21trap "{ touch /${SERVICE}.terminated; exit 1; }" SIGTERM SIGABRT
22
23rm -f "/${SERVICE}".*
24touch "/${SERVICE}.startfail"
25
26systemd-notify EXTEND_TIMEOUT_USEC="$extend_timeout_interval"
27while [[ $start_intervals -gt 0 ]]
28do
29    sleep "$sleep_interval"
30    start_intervals=$((start_intervals - 1))
31    systemd-notify EXTEND_TIMEOUT_USEC="$extend_timeout_interval"
32done
33
34systemd-notify --ready --status="Waiting for your request"
35
36touch "/${SERVICE}.runtimefail"
37rm "/${SERVICE}.startfail"
38
39systemd-notify EXTEND_TIMEOUT_USEC="$extend_timeout_interval"
40while [[ $run_intervals -gt 0 ]]
41do
42    sleep "$sleep_interval"
43    run_intervals=$((run_intervals - 1))
44    systemd-notify EXTEND_TIMEOUT_USEC="$extend_timeout_interval"
45done
46
47systemd-notify STOPPING=1
48
49touch "/${SERVICE}.stopfail"
50rm "/${SERVICE}.runtimefail"
51
52systemd-notify EXTEND_TIMEOUT_USEC="$extend_timeout_interval"
53while [[ $stop_intervals -gt 0 ]]
54do
55    sleep "$sleep_interval"
56    stop_intervals=$((stop_intervals - 1))
57    systemd-notify EXTEND_TIMEOUT_USEC="$extend_timeout_interval"
58done
59
60touch "/${SERVICE}.success"
61rm "/${SERVICE}.stopfail"
62
63exit 0
64