#!/usr/bin/env bash # # Log viewer for email_tunnel systemd service set -euo pipefail # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color SERVICE_NAME="email_tunnel.service" # Check if systemctl is available if ! command -v systemctl &> /dev/null; then echo -e "${RED}Error: systemctl not found${NC}" echo "This script requires systemd." exit 1 fi # Check if journalctl is available if ! command -v journalctl &> /dev/null; then echo -e "${RED}Error: journalctl not found${NC}" echo "This script requires journalctl." exit 1 fi # Function to show help show_help() { cat << EOF ${GREEN}email_tunnel Log Viewer${NC} Usage: $0 [OPTION] Options: -f, --follow Follow logs in real-time (like tail -f) -n, --lines NUMBER Show last N lines (default: 50) -t, --today Show logs from today only -h, --hour Show logs from the last hour -b, --boot Show logs since last boot -a, --all Show all logs (no limit) -s, --status Show service status -e, --errors Show only error messages -v, --verbose Show verbose output (all log levels) --help Show this help message Examples: $0 Show last 50 log lines $0 -f Follow logs in real-time $0 -n 100 Show last 100 lines $0 -t Show today's logs $0 -e Show only errors $0 -s Show service status EOF } # Function to show status show_status() { echo -e "${BLUE}=== Service Status ===${NC}" systemctl status "$SERVICE_NAME" --no-pager -l || true echo "" echo -e "${BLUE}=== Recent Runs ===${NC}" journalctl -u "$SERVICE_NAME" -n 5 --no-pager -o short-precise } # Parse command line arguments FOLLOW=false LINES=50 SINCE="" PRIORITY="" SHOW_ALL=false SHOW_STATUS=false if [ $# -eq 0 ]; then : else while [[ $# -gt 0 ]]; do case $1 in -f|--follow) FOLLOW=true shift ;; -n|--lines) LINES="$2" shift 2 ;; -t|--today) SINCE="today" shift ;; -h|--hour) SINCE="1 hour ago" shift ;; -b|--boot) SINCE="boot" shift ;; -a|--all) SHOW_ALL=true shift ;; -s|--status) SHOW_STATUS=true shift ;; -e|--errors) PRIORITY="err" shift ;; -v|--verbose) PRIORITY="" shift ;; --help) show_help exit 0 ;; *) echo -e "${RED}Error: Unknown option: $1${NC}" echo "Use --help for usage information" exit 1 ;; esac done fi # Check if service exists if ! systemctl list-unit-files | grep -q "$SERVICE_NAME"; then echo -e "${YELLOW}Warning: $SERVICE_NAME is not installed${NC}" echo "Install the service first with: ./install_email_tunnel.sh" exit 1 fi # Show status if requested if [ "$SHOW_STATUS" = true ]; then show_status exit 0 fi # Build journalctl command CMD="journalctl -u $SERVICE_NAME" # Add follow flag if [ "$FOLLOW" = true ]; then CMD="$CMD -f" fi # Add line limit if [ "$SHOW_ALL" = false ] && [ "$FOLLOW" = false ]; then CMD="$CMD -n $LINES" fi # Add time filter if [ -n "$SINCE" ]; then if [ "$SINCE" = "boot" ]; then CMD="$CMD -b" else CMD="$CMD --since \"$SINCE\"" fi fi # Add priority filter if [ -n "$PRIORITY" ]; then CMD="$CMD -p $PRIORITY" fi # Add no-pager for non-follow mode if [ "$FOLLOW" = false ]; then CMD="$CMD --no-pager" fi # Show what we're doing if [ "$FOLLOW" = true ]; then echo -e "${GREEN}Following logs for $SERVICE_NAME...${NC}" echo "Press Ctrl+C to stop" echo "" elif [ -n "$SINCE" ]; then echo -e "${GREEN}Showing logs since $SINCE...${NC}" echo "" elif [ "$SHOW_ALL" = true ]; then echo -e "${GREEN}Showing all logs for $SERVICE_NAME...${NC}" echo "" else echo -e "${GREEN}Showing last $LINES log lines for $SERVICE_NAME...${NC}" echo "" fi # Execute the command eval "$CMD"