Skip to content

Instantly share code, notes, and snippets.

@tonusoo
Created November 12, 2025 12:35
Show Gist options
  • Select an option

  • Save tonusoo/e58728cdd0785978b00593568d5b6687 to your computer and use it in GitHub Desktop.

Select an option

Save tonusoo/e58728cdd0785978b00593568d5b6687 to your computer and use it in GitHub Desktop.
logtail functionality for journalctl versions older than 242 not supporting the "--cursor-file" option
#!/usr/bin/env bash
get_journal_logs() {
log_type="$1"
if [[ "$log_type" == "system" ]]; then
args+=( "--dmesg" )
elif [[ "$log_type" == "apache" ]]; then
args+=( "--unit=apache2" )
fi
cursor_file="$HOME/.journal_cursor_${log_type}"
if [[ -f "$cursor_file" ]]; then
read -r cursor < "$cursor_file"
[[ -n "$cursor" ]] && args+=( "--after-cursor" "$cursor" )
fi
args+=( "--no-pager" )
args+=( "--show-cursor" )
args+=( "--lines=1000" )
while read -r logline; do
case "$logline" in
"-- cursor:"*)
echo "${logline#-- cursor: }" > "$cursor_file"
;;
# Supress the "-- No entries --" message.
# Avoid using --quiet which suppresses all
# informational messages.
"-- No entries --")
;;
*)
echo "$logline"
;;
esac
done < <(journalctl "${args[@]}")
}
get_journal_logs "system" | \
mailx --discard-empty-messages -s "system log check" [email protected]
get_journal_logs "apache" | \
mailx --discard-empty-messages -s "Apache log check" [email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment