Da ich viel einzelne Configs habe, hilft dieses Script den Workflow einfacher zu halten. Geschrieben mit Opus 4. Prompt: write a short bash shell script that reloads nginx and tests the config before the command for the reload is service nginx reload the test is nginx -t since the nginx config is fragmented acress a number of referenced files, help me figure out which part has the config error and show the the path to the file
39 lines
No EOL
982 B
Bash
39 lines
No EOL
982 B
Bash
#!/bin/bash
|
|
|
|
# nginx-safe-reload.sh
|
|
# Safely reload nginx by testing configuration first
|
|
|
|
echo "Testing nginx configuration..."
|
|
|
|
# Test nginx configuration and capture output
|
|
test_output=$(nginx -t 2>&1)
|
|
test_result=$?
|
|
|
|
if [ $test_result -eq 0 ]; then
|
|
echo "✓ Configuration test passed"
|
|
echo "Reloading nginx..."
|
|
|
|
# Reload nginx service
|
|
if service nginx reload; then
|
|
echo "✓ nginx reloaded successfully"
|
|
exit 0
|
|
else
|
|
echo "✗ Failed to reload nginx service"
|
|
exit 1
|
|
fi
|
|
else
|
|
echo "✗ Configuration test failed - nginx NOT reloaded"
|
|
echo
|
|
echo "Error details:"
|
|
echo "$test_output"
|
|
echo
|
|
|
|
# Try to extract and highlight the problematic file
|
|
error_file=$(echo "$test_output" | grep -o 'in /[^:]*' | head -1 | sed 's/in //')
|
|
if [ -n "$error_file" ]; then
|
|
echo "🔍 Check this file: $error_file"
|
|
fi
|
|
|
|
echo "Please fix configuration errors before reloading"
|
|
exit 1
|
|
fi |