-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload_crontab_workstation.sh
executable file
·95 lines (78 loc) · 2.04 KB
/
load_crontab_workstation.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
# load crontab from crontab-workstation.txt
#
#
CRON_USER=ernie
FILEHOME=/Users/ernie/git/utilities
HOSTNAME_PATTERN1="imac"
HOSTNAME_PATTERN2="mba"
HOSTNAME_PATTERN3="mbp"
HOSTNAME_PATTERN4="mb12"
autoupdate_version = 6
if [ "$CRON_USER" != "$USER" ]
then
command="sudo crontab -u $CRON_USER"
else
command="crontab"
fi
if [ ! "`hostname -s | grep $HOSTNAME_PATTERN1`" -a ! "`hostname -s | grep $HOSTNAME_PATTERN2`" -a ! "`hostname -s | grep $HOSTNAME_PATTERN3`" -a ! "`hostname -s | grep $HOSTNAME_PATTERN4`" ]
then
echo "Incorrect hostname: (looks like: $HOSTNAME, should contain: \"$HOSTNAME_PATTERN1\" or \"$HOSTNAME_PATTERN2\" or \"$HOSTNAME_PATTERN3\" or \"$HOSTNAME_PATTERN4\")"
exit 3
fi
if [ ! -e "$FILEHOME/crontab-workstation.txt" ]
then
echo "Can't find crontab-workstation.txt in $FILEHOME"
exit 2
fi
if ! which -s ts
then
# for "ts" command
#
brew install moreutils
fi
tempfile1=$(mktemp)
tempfile2=$(mktemp)
#cat "$FILEHOME/crontab-workstation.txt" | $command
crontab_file="$FILEHOME/crontab-workstation.txt"
# Save old crontab for comparison before overwriting
#
$command -l > $tempfile1
crontab_env_file="$FILEHOME/crontab.env"
echo -n > "$tempfile2"
if [ -e "$crontab_env_file" ]
then
echo "# Below loaded from $crontab_env_file" >> "$tempfile2"
echo "#" >> "$tempfile2"
cat "$crontab_env_file" >> $tempfile2
fi
echo "" >> "$tempfile2"
echo "# Below loaded from $crontab_file" >> "$tempfile2"
echo "#" >> "$tempfile2"
cat "$crontab_file" >> $tempfile2
# Load machine specific addition if present
#
extra_file="$FILEHOME/crontab-workstation-$(hostname -s).txt"
if [ -e "$extra_file" ]
then
echo >> "$tempfile2"
echo "# Below loaded from $extra_file" >> "$tempfile2"
echo "#" >> "$tempfile2"
cat "$extra_file" >> "$tempfile2"
fi
if [[ ! "$(diff $tempfile1 $tempfile2)" ]]
then
echo "No updates. Aborting."
exit 1
fi
diff $tempfile1 $tempfile2
echo -n "Diff ok? (y/n): "
read ans
if [[ "$ans" == "y" ]]
then
echo "Loading crontab."
cat "$tempfile2" | $command
else
echo Aborting.
fi
rm $tempfile1 $tempfile2