-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.sh
executable file
·77 lines (61 loc) · 1.65 KB
/
timer.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/sh
# shellcheck source-path=SCRIPTDIR
CURRENT_DIR=$(dirname "$(readlink -f "$0")")
SCRIPTS_PATH="$CURRENT_DIR"
. "$SCRIPTS_PATH/helpers.sh"
LOCKFILE="/tmp/muxtab.timer.lock"
main() {
# acquire lock
PID_LOCKFILE=${LOCKFILE}.$$
touch ${PID_LOCKFILE}
ln -s ${PID_LOCKFILE} ${LOCKFILE} 2>/dev/null
ret=$?
if [ "${ret}" -ne 0 ]; then
# another timer is running
rm -f ${PID_LOCKFILE}
exit 0
fi
trap 'rm -f ${LOCKFILE}; exit 0' INT TERM EXIT
current_time=$(date +%s)
muxtab_file=$(get_tmux_option "@muxtab_file" "$HOME/.muxtab")
if [ ! -e "$muxtab_file" ]; then
# no muxtab file found
exit 0
fi
# loop through the muxtab file
while read -r line; do
# remove leading whitespaces
cleaned_line=$(echo "$line" | sed -e 's/^[[:space:]]*//')
# skip empty lines
if [ -z "$cleaned_line" ]; then
continue
fi
# skip comments
case "$cleaned_line" in
\#*) continue ;;
esac
# split line into interval and command
interval=$(echo "$cleaned_line" | awk '{print $1}')
command=$(echo "$cleaned_line" | awk '{$1=""; print $0}' | sed -e 's/^[[:space:]]*//')
# check if interval is a number
case "$interval" in
'' | *[!0-9]*) continue ;;
esac
# check if the command is due to be run
hash=$(echo "$command" | sha256sum | cut -d' ' -f1)
if [ -e "/tmp/muxtab.timer.$hash" ]; then
last_time=$(cat "/tmp/muxtab.timer.$hash")
if [ $((last_time + interval)) -gt "$current_time" ]; then
# not due yet
continue
fi
fi
# run command
echo "$current_time" >"/tmp/muxtab.timer.$hash"
eval "$command" 1>/dev/null 2>/dev/null &
done <"$muxtab_file"
# release lock
rm -f ${LOCKFILE}
rm -f ${PID_LOCKFILE}
}
main