forked from MPOS/php-mpos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-maintenance.sh
executable file
·148 lines (123 loc) · 3.49 KB
/
run-maintenance.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash
#########################
# #
# Configuration Options #
# #
#########################
# PHP Detections, if this fails hard code it
PHP_BIN=$( which php )
# List of cruns to execute
CRONS="tickerupdate.php notifications.php tables_cleanup.php"
# Output additional runtime information
VERBOSE="0"
# Base path for PIDFILE, (full path).
BASEPATH="/tmp"
# Subfolder for PIDFILE, so it's path will be unique in a multipool server.
# Path relative to BASEPATH.
# Eg. SUBFOLDER="LTC"
SUBFOLDER=""
################################################################
# #
# You probably don't need to change anything beyond this point #
# #
################################################################
# Mac OS detection
OS=`uname`
case "$OS" in
Darwin) READLINK=$( which greadlink ) ;;
*) READLINK=$( which readlink ) ;;
esac
if [[ ! -x $READLINK ]]; then
echo "readlink not found, please install first";
exit 1;
fi
# My own name
ME=$( basename $0 )
# Overwrite some settings via command line arguments
while getopts "hfvt:p:d:" opt; do
case "$opt" in
h|\?)
echo "Usage: $0 [-v] [-f] [-t TIME_IN_SEC] [-p PHP_BINARY] [-d SUBFOLDER]";
exit 0
;;
v) VERBOSE=1 ;;
f) PHP_OPTS="$PHP_OPTS -f";;
p) PHP_BIN=$OPTARG ;;
d) SUBFOLDER=$OPTARG ;;
t)
if [[ $OPTARG =~ ^[0-9]+$ ]]; then
TIMEOUT=$OPTARG
PHP_OPTS="$PHP_OPTS -t $OPTARG"
else
echo "Option -t requires an integer" >&2
exit 1
fi
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
# Path to PID file, needs to be writable by user running this
PIDFILE="${BASEPATH}/${SUBFOLDER}/${ME}.pid"
# Clean PIDFILE path
PIDFILE=$($READLINK -m "$PIDFILE")
# Create folders recursively if necessary
if ! $(mkdir -p $( dirname $PIDFILE)); then
echo "Error creating PIDFILE path: $( dirname $PIDFILE )"
exit 1
fi
# Find scripts path
if [[ -L $0 ]]; then
CRONHOME=$( dirname $( $READLINK $0 ) )
else
CRONHOME=$( dirname $0 )
fi
# Change working director to CRONHOME
if ! cd $CRONHOME 2>/dev/null; then
echo "Unable to change to working directory \$CRONHOME: $CRONHOME"
exit 1
fi
# Confiuration checks
if [[ -z $PHP_BIN || ! -x $PHP_BIN ]]; then
echo "Unable to locate you php binary."
exit 1
fi
if [[ ! -e 'shared.inc.php' ]]; then
echo "Not in cronjobs folder, please ensure \$CRONHOME is set!"
exit 1
fi
# Our PID of this shell
PID=$$
# If $PIDFILE exists and older than the time specified by -t, remove it.
if [[ -e $PIDFILE ]]; then
if [[ -n $TIMEOUT ]] && \
[[ $(( $(date +%s) - $(stat -c %Y $PIDFILE) )) -gt $TIMEOUT ]]; then
echo "$PIDFILE exists but older than the time you specified in -t option ($TIMEOUT sec)."
echo "Removing PID file."
rm $PIDFILE
fi
fi
if [[ -e $PIDFILE ]]; then
echo "Cron seems to be running already"
RUNPID=$( cat $PIDFILE )
if ps fax | grep -q "^\<$RUNPID\>"; then
echo "Process found in process table, aborting"
exit 1
else
echo "Process $RUNPID not found. Plese remove $PIDFILE if process is indeed dead."
exit 1
fi
fi
# Write our PID file
echo $PID 2>/dev/null 1> $PIDFILE || {
echo 'Failed to create PID file, aborting';
exit 1
}
for cron in $CRONS; do
[[ $VERBOSE == 1 ]] && echo "Running $cron, check logfile for details"
$PHP_BIN $cron $PHP_OPTS
done
# Remove pidfile
rm -f $PIDFILE