chiark / gitweb /
ubuntu-daily: no more ports directories
[bin.git] / crondiv
1 #! /bin/bash
2
3 # "Divide" a crontab frequency down by the first argument on the command line,
4 # logging to the second argument, and executing the remainder.
5
6 if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
7         echo "Usage: $0 frequency logfile command [ arguments ]" >&2
8         exit 1
9 fi
10
11 FREQUENCY="$1"
12 LOGFILE="$2"
13 shift 2
14
15 if [ -f "$LOGFILE" ]; then
16         LASTEXEC="$(($(tail -n 1 "$LOGFILE")+1))"
17 else
18         LASTEXEC=1
19 fi
20
21 if [ "$LASTEXEC" -ge "$FREQUENCY" ]; then
22         echo "$* executed at $(date)" >> "$LOGFILE"
23         echo 0 >> "$LOGFILE"
24         eval "$*"
25 else
26         echo "$LASTEXEC" >> "$LOGFILE"
27 fi
28