claude-home/examples/bash/service-management.md
Cal Corum 2bfbc7bb77 CLAUDE: Initialize efficient documentation system for home lab
- Created structured documentation with /patterns/, /examples/, and /reference/ directories
- Implemented automatic context loading rules in CLAUDE.md based on file extensions, directories, and keywords
- Added technology-specific patterns for Docker, Python, Node.js, Vue.js, Bash, networking, databases, and VM management
- Included complete working examples for common workflows and troubleshooting references
- Designed for minimal context usage with precise loading triggers

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-08 19:59:29 -05:00

2.9 KiB

Bash Service Management Examples

Systemd Service Script

#!/bin/bash
set -euo pipefail

SERVICE_NAME="myapp"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"

usage() {
    echo "Usage: $0 {install|start|stop|restart|status|logs|uninstall}"
    exit 1
}

install_service() {
    echo "Installing ${SERVICE_NAME} service..."
    
    sudo tee "$SERVICE_FILE" > /dev/null << EOF
[Unit]
Description=My Application Service
After=network.target
Wants=network.target

[Service]
Type=simple
User=myapp
Group=myapp
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/bin/myapp
Restart=always
RestartSec=10
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target
EOF

    sudo systemctl daemon-reload
    sudo systemctl enable "$SERVICE_NAME"
    echo "Service installed and enabled"
}

manage_service() {
    local action="$1"
    echo "${action^}ing ${SERVICE_NAME} service..."
    sudo systemctl "$action" "$SERVICE_NAME"
    echo "Service ${action}ed successfully"
}

show_status() {
    echo "=== Service Status ==="
    sudo systemctl status "$SERVICE_NAME" --no-pager -l
    echo
    echo "=== Service Logs (last 20 lines) ==="
    sudo journalctl -u "$SERVICE_NAME" --no-pager -n 20
}

show_logs() {
    echo "Following logs for ${SERVICE_NAME}..."
    sudo journalctl -u "$SERVICE_NAME" -f
}

uninstall_service() {
    echo "Uninstalling ${SERVICE_NAME} service..."
    sudo systemctl stop "$SERVICE_NAME" || true
    sudo systemctl disable "$SERVICE_NAME" || true
    sudo rm -f "$SERVICE_FILE"
    sudo systemctl daemon-reload
    echo "Service uninstalled"
}

case "${1:-}" in
    install)
        install_service
        ;;
    start|stop|restart)
        manage_service "$1"
        ;;
    status)
        show_status
        ;;
    logs)
        show_logs
        ;;
    uninstall)
        uninstall_service
        ;;
    *)
        usage
        ;;
esac

Process Monitoring Script

#!/bin/bash
set -euo pipefail

PROCESS_NAME="myapp"
RESTART_COMMAND="/opt/myapp/start.sh"
LOG_FILE="/var/log/myapp-monitor.log"
PID_FILE="/var/run/myapp.pid"

log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}

check_process() {
    if [[ -f "$PID_FILE" ]]; then
        local pid=$(cat "$PID_FILE")
        if kill -0 "$pid" 2>/dev/null; then
            return 0  # Process is running
        else
            rm -f "$PID_FILE"
            return 1  # Process is not running
        fi
    else
        return 1  # PID file doesn't exist
    fi
}

restart_process() {
    log_message "Attempting to restart $PROCESS_NAME..."
    if $RESTART_COMMAND; then
        log_message "$PROCESS_NAME restarted successfully"
    else
        log_message "Failed to restart $PROCESS_NAME"
        exit 1
    fi
}

main() {
    if ! check_process; then
        log_message "$PROCESS_NAME is not running"
        restart_process
    else
        log_message "$PROCESS_NAME is running normally"
    fi
}

main "$@"