-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun_from_cron.sh
More file actions
executable file
·100 lines (83 loc) · 2.5 KB
/
run_from_cron.sh
File metadata and controls
executable file
·100 lines (83 loc) · 2.5 KB
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
96
97
98
99
#!/bin/sh
my_dir="`dirname \"$0\"`"
cd "$my_dir"
if [ $? -ne 0 ] ; then
echo "Could not cd to $my_dir" >&2
exit 5
fi
. $my_dir/XCAB.settings
. $my_dir/functions.sh
my_name="`basename \"$0\"`"
if [ ! -d "$PREFS_DIR" ] ; then
mkdir -p "$PREFS_DIR"
fi
if [ ! -d "$PREFS_DIR/logs" ] ; then
mkdir -p "$PREFS_DIR/logs"
fi
if [ ! -d "$PREFS_DIR/run" ] ; then
mkdir -p "$PREFS_DIR/run"
fi
TEMPFILE="$PREFS_DIR/run/$my_name.$$"
LOCKFILE="$PREFS_DIR/run/$my_name.lock"
START_LOG="$PREFS_DIR/logs/${my_name}_start.log"
RUN_LOG="$PREFS_DIR/logs/${my_name}.log"
#Make a temp file with our pid in it
echo $$ > "$TEMPFILE" 2>/dev/null
if [ $? -ne 0 ] ; then
echo "Could not create $TEMPFILE" >&2
echo "Could not create $TEMPFILE" | mail -s "$0 error" "$ERROR_EMAIL"
exit 5
fi
#Now atomically create our lockfile
ln "$TEMPFILE" "$LOCKFILE" > /dev/null 2>&1
if [ $? -eq 0 ] ; then
#lockfile created, remove the temp file, then continue on
rm -f "$TEMPFILE"
else
#Couldn't create the lock file, see if the process that made it is still around
# by sending it a dummy signal
kill -0 `cat "$LOCKFILE"` > /dev/null 2>&1
if [ $? -eq 0 ] ; then
#process received the signal, so still running, so we give up
rm -f "$TEMPFILE"
exit 0
else
echo "Removing stale lock file"
rm -f "$LOCKFILE"
#Now atomically create our lockfile again
ln "$TEMPFILE" "$LOCKFILE" > /dev/null 2>&1
if [ $? -eq 0 ] ; then
#lockfile created, remove the temp file, then continue on
rm -f "$TEMPFILE"
else
echo "Could not create lock file, even after removing stale one" >&2
echo "Could not create lock file, even after removing stale one" | mail -s "$0 error" "$ERROR_EMAIL"
rm -f "$TEMPFILE"
exit 5
fi
fi
fi
echo "$0 run starting at `date`" > "$START_LOG" 2>&1
rm "$RUN_LOG" && touch "$RUN_LOG"
#Only want to see errors
git fetch >/dev/null 2>&1
git pull --rebase origin master >/dev/null 2>&1
if [ $? -ne 0 ] ; then
echo "Error pulling from master" >&2
git fetch >> "$RUN_LOG" 2>&1
git status >> "$RUN_LOG" 2>&1
git pull --rebase origin master >> "$RUN_LOG" 2>&1
echo "Error pulling from master" >> "$RUN_LOG"
cat "$START_LOG" "$RUN_LOG" | mail -s "$my_dir error and log" "$SUCCESS_EMAIL"
rm -f "$LOCKFILE"
exit 4
fi
git fetch >/dev/null 2>&1
$my_dir/sync_from_Dropbox.sh >> "$RUN_LOG" 2>&1
$my_dir/build_and_notify.sh >> "$RUN_LOG" 2>&1
#Only send mail if there's something to report
if [ -s "$RUN_LOG" ] ; then
cat "$START_LOG" "$RUN_LOG" | mail -s "$my_dir log" "$SUCCESS_EMAIL"
rm -f "$LOCKFILE"
fi
rm -f "$LOCKFILE"