-cursor=$(journalctl --show-cursor -qn0|sed 's/^\s*--\scursor:\s*//')
-if "$@"; then
- if [[ $file ]]; then
- rm -f $file
- if [[ $file == $c$errors ]]; then
- echo | mail -s "$HOSTNAME: $service success" $u@localhost
- fi
+if [[ ! $file ]]; then
+ cursor=$(journalctl --show-cursor -qn0|sed 's/^\s*--\scursor:\s*//')
+fi
+
+code=0
+"$@" || code=$?
+if (( code )); then
+ send_mail=false
+ if [[ $file ]]; then
+ i=${file#$c}
+ if (( i < errors )); then
+ new_file=$c$((i+1))
+ mv $file $new_file
+ file=$new_file
+ if [[ $file == $c$errors ]]; then
+ send_mail=true
+ fi
+ fi
+ else
+ file=${c}1
+ printf "%s\n" "$cursor" >$file
+ if (( errors == 1 )); then
+ send_mail=true