-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmbsync-launcher.sh
More file actions
executable file
·215 lines (197 loc) · 7.78 KB
/
mbsync-launcher.sh
File metadata and controls
executable file
·215 lines (197 loc) · 7.78 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env bash
### BEGIN INIT INFO
# Provides: msbync-launcher
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO
# /*------------------------------------------------------------------------*\
# | |
# | WTFPL & DSSL apply on this material. |
# | |
# +--------------------------------------------------------------------------+
# | |
# | mbsync-launcher : A script to sync your mail accounts via mbsync. |
# | Copyright (C) 2017-2017 Flyounet — Tous droits réservés. |
# | |
# | Cette œuvre est distribuée SANS AUCUNE GARANTIE hormis celle d'être |
# | distribuée sous les termes de la Licence Demerdez-vous («Demerden Sie |
# | Sich License») telle que publiée par Flyounet : soit la version 1 de |
# | cette licence,soit (à votre gré) toute version ultérieure. |
# | telle que publiée par Flyounet : soit la version 1 de cette licence, |
# | soit (à votre gré) toute version ultérieure. |
# | |
# | Vous devriez avoir reçu une copie de la Licence Démerdez-vous avec cette |
# | œuvre ; si ce n’est pas le cas, consultez : |
# | <http://dssl.flyounet.net/licenses/>. |
# | |
# \*------------------------------------------------------------------------*/
# a=0;while true; do mbsync -c ../mbsync/mbsync.conf -a ; [[ $((++a % 5)) == 0 ]] && sleep 120; done
_configFile="${HOME}/sync/syncConfig/mails/mbsync/mbsync.conf"
# Where is stored your mbsync config file
_excludeChannel="channel-mail-test,channel-sent-test"
# If some channels don't need to be managed
# by mbsync (comma separated list)
_tmpDir='/tmp' # Where will be stored temp and logs files
_pidFile="${_tmpDir}/.mbsl.pid" # Pid file used to know if script is running
_timeWaitSecondBeforeKill=180 # mbsync has 3 minutes to complete before being
# killed
_timeToThink=10 # Instead of waiting 3 minutes, we check every
# 10 seconds for mbsync completion
_timeLittleMore=30 # If we're near time to kill mbsync, we also
# check if mbsync logs are updated. If mbsync
# still works, we give some time more to
# complete. Default 30 seconds
_timeHardLimit=300 # If mbsync runs for more than Hard Limit 5 min
# mbsync is killed.
_waitBetweenLaunch=120 # Wait x seconds before restarting
_parallelRun=0 # Could be useful if you more than one channel
# in your mbsync config.
# 0 => No parallel run
# 1 => All channel launched in parallel
# >1 => max concurrent channel in parallel
_user="${USER}" # The user which runs your mbsync
RUN_ONCE=${RUN_ONCE:=1} # Only run once and don't work for every time
VERSION='0.0.3' # Script Info
#
# Exit and print message
#
die () {
printf "%s\n" "${@//%/%%}" >&2
exit 1
}
#
# Generate a file with pid of running process
#
_createLockFile () {
local _spid="${$}"
[[ ! -z "${1:-}" ]] && _spid="${1}"
printf '%s' "${_spid}::$(sed -e 's;\x0;;g' "/proc/${_spid}/cmdline")" > "${_pidFile}" || die 'Unable to create lock. Aborting...';
}
#
# Check if pid file is there and return 0 if running
#
_verifyLockFile () {
[[ -e "${_pidFile}" && -s "${_pidFile}" ]] && {
local _pid="$(< "${_pidFile}")"
grep -qE "^${$}::" <<< "${_pid}" || {
[[ "$(sed -e 's;\x0;;g' "/proc/${_pid%::*}/cmdline" 2>/dev/null)" = "${_pid##*::}" ]] || return 1
export _runningPid="${_pid%::*}"
return 0
}
export _runningPid="${_pid%::*}"
return 0
}
return 1
}
#
# Real execution
#
_startExecution () {
trap 'rm -f -- "${_pidFile}" "${_tmpDir}"/.channel-*' INT EXIT
while true; do
export _date="$(date "+%Y%m%d")"
[[ -e "${_pidFile}.stop" ]] && break
_maxConcurrent=0
[[ ${_parallelMode:=0} -gt 1 ]] && { _maxConcurrent=${_parallelMode}; }
while read channel; do
(
[[ -e "${_pidFile}.stop" ]] && return 0
[[ ! -z "${1:-}" ]] && { [[ "${1:-}" != "${channel}" ]] && continue; }
[[ -f "${_tmpDir}/.channel-${channel}" ]] && continue
:> "${_tmpDir}/.channel-${channel}"
echo "##################### Starting @ $(date)" >> "${_tmpDir}/log.${channel}.${_date}"
( mbsync -c "${_configFile}" "${channel}" &>>"/tmp/log.${channel}.${_date}" ) &
_mbsyncPid=${!}
waitToKill ${_mbsyncPid} ${channel} &
_waiter=${!}
wait ${_mbsyncPid}
) &
_channelWait=${!}
sleep 1
[[ ${_parallelMode:=0} -eq 1 ]] && continue
wait ${_channelWait}
done <<< "${_channels}"
[[ -e "${_pidFile}.stop" ]] && break
[[ ${RUN_ONCE} -eq 0 ]] && break
sleep ${_waitBetweenLaunch:=120}
done
rm -f -- "${_pidFile}.stop" "${_pidFile}"
}
#
# Wait and kill process
#
waitToKill () {
local _pid=${1:-}
local _channel=${2:-}
[[ -z "${_pid}" || ${_pid} -eq 1 ]] && _die "God or empty Pid. Aborting..."
[[ -z "${_channel}" ]] && _die "Channel name empty. Aborting..."
local _nowStart="$(date "+%s")"; local _now="${_nowStart}"
local _prevState=''
while (( $(date "+%s") < _now + _timeWaitSecondBeforeKill)); do
[[ -s "${_pidFile}.stop" ]] && break
ps --pid ${_pid} &>/dev/null && sleep ${_thinkTime:=10}
ps --pid ${_pid} &>/dev/null || break
local _state="$(tail -1 "${_tmpDir}/log.${_channel}.${_date}" | sha1sum)"
[[ "${_prevState}" != "${_state}" ]] && {
_prevState="${_state}"
(( $(date "+%s") + _timeLittleMore >= _now )) && (( _now+=_timeLittleMore ))
}
(( $(date "+%s") > _nowStart + _timeHardLimit )) && break
done
ps -u "${_user}" --pid ${_pid} &>/dev/null && kill -9 ${_pid} &>/dev/null
rm -f -- "${_tmpDir}/.channel-${_channel}"
echo "##################### Ending @ $(date)" >> "${_tmpDir}/log.${_channel}.${_date}"
}
#
# Print the status
#
_status () {
_verifyLockFile || die 'No process are running'
printf '%s\n' "Process (${_runningPid}) is currently running."
[[ ${_force:=0} -eq 1 ]] && { printf '%s\n' ' --> Currently working on channel(s) :'; for c in "${_tmpDir}"/.channel-* ; do [[ ! -z "${c}" && "${c##*.channel-}" != '*' ]] && printf '%s\n' " - ${c##*.channel-}"; done; }
exit 0
}
#
# Stop the process
#
_stop () {
[[ -e "${_pidFile}.stop" ]] && printf '%s\n' 'Stop was already requested.'
printf '%s\n' "Requesting stop. Please wait..."
:> "${_pidFile}.stop"
[[ ${_force:=0} -eq 1 ]] && printf '%s' 'force' > "${_pidFile}.stop"
sleep ${_thinkTime:=10}
[[ ! -e "${_pidFile}" ]] && printf '%s\n' 'All process should have been stopped'
}
#
# Start the process
#
_start () {
_verifyLockFile && die "Process (${_runningPid}) is currently running."
_createLockFile
rm -f -- "${_pidFile}.stop"
( _startExecution "${1:-}" &>/dev/null ) &
_createLockFile "${!}"
sleep 2
_status
}
# List of account
[[ -e "${_configFile}" ]] || die "No configuration file. Aborting..."
_channels="$(sed -e '/^[[:space:]]*Channel/!d;s/^[[:space:]]*Channel[[:space:]]*//' "${_configFile}")"
# Check if there is any channel in the config file...
[[ -z "${_channels// /}" ]] && die "No channel in '${_configFile}'"
_argv="${1:-}"
[[ ${#} -gt 1 ]] && { shift; _requester="${@}"; }
case "${_argv,,}" in
start) _start "${_requester:-}" &>/dev/null;;
stop) _stop;;
forcestop) _force=1 _stop;;
info) _force=1 _status;;
status) _status;;
restart) _force=1 _stop; sleep 2; _start "${_requester:-}" &>/dev/null;;
version) printf '%s\n' "${0} v${VERSION}";;
*) echo "usage: ${0} [start [channel-name]|status|info|stop|forcestop|restart]" >&2 ;;
esac
exit 0
# $Format:%cn @ %cD$ : %h $