forked from rhiever/TwitterFollowBot
-
Notifications
You must be signed in to change notification settings - Fork 19
/
run.sh
executable file
·66 lines (55 loc) · 1.55 KB
/
run.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
#!/bin/bash
THIS_SCRIPT=$(readlink -f $0)
THIS_DIR=$(dirname ${THIS_SCRIPT})
DATE_NOW=$(date +%Y-%m-%d_%H-%M-%S)
LOG_DIR=${THIS_DIR}/log
function setup_log_output {
mkdir -p ${LOG_DIR}
LOG_FILE=${LOG_DIR}/${DATE_NOW}.log
ln -sf ${LOG_FILE} ${LOG_DIR}/latest
}
function setup_virtualenv {
VENV_DIR=${THIS_DIR}/venv
if [ ! -d "${VENV_DIR}" ]; then
if [ -s "${THIS_DIR}/.python_version" ]; then
virtualenv ${VENV_DIR} -p "$(cat ${THIS_DIR}/.python_version)" >> ${LOG_FILE}
else
virtualenv ${VENV_DIR} >> ${LOG_FILE}
fi
fi
source ${THIS_DIR}/venv/bin/activate
}
function install_dependencies {
pip install -r ${THIS_DIR}/requirements.txt >> ${LOG_FILE}
}
function source_settings_and_credentials {
for ENV_FILE in settings.sh credentials.sh
do
if [ -s "${THIS_DIR}/${ENV_FILE}" ]; then
source "${THIS_DIR}/${ENV_FILE}"
fi
done
}
function delete_old_logs {
find ${LOG_DIR} -type f -iname '*.log' -mtime +30 -delete
}
function run_main_code {
export PYTHONIOENCODING="utf-8"
command=${THIS_DIR}/main.py
# Line buffering, see http://unix.stackexchange.com/a/25378
stdbuf -oL -eL run-one ${command} >> ${LOG_FILE} 2>&1
RETCODE=$?
if [ ${RETCODE} != 0 ]; then
echo "$@ exited with code: ${RETCODE}"
git remote -v
tail -v -n 100 ${LOG_FILE}
exit 2
fi
grep -n '^ERROR:' ${LOG_FILE}
}
setup_log_output
setup_virtualenv
install_dependencies
source_settings_and_credentials
run_main_code
delete_old_logs