#!/bin/bash # ----------------------------------------------------------------------------------------------- # ----------------------------- PHP-FPM Max Children Script ------------------------------------ # ----------------------------------------------------------------------------------------------- # Persistently sets PHP-FPM pm.max_children on unRAID. # Config file path and max children value are set in Master.conf. # Supports --dry-run to preview what would be changed without making changes. # ----------------------------------------------------------------------------------------------- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/../Master.conf" source "$SCRIPT_DIR/../common.sh" parse_args "$@" # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_GEAR Setup ━━━ # ----------------------------------------------------------------------------------------------- echo "" echo "━━━ $ICON_GEAR Setup ━━━" # ROOT CHECK if [[ "$EUID" -ne 0 ]]; then error "Must be run as root" exit 1 fi success "Running as root" # VALIDATION validate_int PHP_MAX_CHILDREN "$PHP_MAX_CHILDREN" require_var PHP_CONF # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_SUMMARY Status ━━━ # ----------------------------------------------------------------------------------------------- if [[ "$SHOW_STATUS" == true ]]; then echo "" echo "━━━━━ $ICON_SUMMARY STATUS ━━━━━" echo "$ICON_GEAR Config File: $PHP_CONF" echo "$ICON_PHP Max Children: $PHP_MAX_CHILDREN" echo "$ICON_GEAR Dry Run: $DRY_RUN" echo "━━━━━━━━━━━━━━━━━━━━━━━" exit 0 fi # ----------------------------------------------------------------------------------------------- # FUNCTIONS # ----------------------------------------------------------------------------------------------- # Applies pm.max_children to the PHP-FPM config file and restarts the service. # Verifies the value was applied correctly after restart. # Skips all changes if dry run is active. apply_php_max_children() { local target="pm.max_children = $PHP_MAX_CHILDREN" if [[ "$DRY_RUN" == true ]]; then warn "DRY RUN — would set pm.max_children = $PHP_MAX_CHILDREN in $PHP_CONF" warn "DRY RUN — would restart PHP-FPM service" return 0 fi # Verify config file exists before attempting changes if [[ ! -f "$PHP_CONF" ]]; then error "PHP config file not found: $PHP_CONF" return 1 fi info "Applying pm.max_children = $PHP_MAX_CHILDREN..." if ! sed -i "s/^pm\.max_children.*/$target/" "$PHP_CONF"; then error "Failed to update PHP config: $PHP_CONF" return 1 fi success "Config updated" info "Restarting PHP-FPM..." if ! /etc/rc.d/rc.php-fpm restart; then error "PHP-FPM restart failed" return 1 fi success "PHP-FPM restarted" # Verify the value was applied correctly local current current=$(grep -E "^pm\.max_children" "$PHP_CONF" || true) if [[ -n "$current" ]]; then success "Verified: $current" logger "Userscript: PHP-FPM updated → $current" else warn "Could not verify configuration value — check $PHP_CONF manually" fi return 0 } # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_PHP PHP-FPM Config ━━━ # ----------------------------------------------------------------------------------------------- echo "" echo "━━━ $ICON_PHP PHP-FPM Config ━━━" echo "$ICON_GEAR Config File: $PHP_CONF" echo "$ICON_PHP Max Children: $PHP_MAX_CHILDREN" echo "$ICON_GEAR Dry Run: $DRY_RUN" echo "" [[ "$DRY_RUN" == true ]] && warn "DRY RUN — no changes will be made" START=$(date +%s) PHP_SUCCESS=false apply_php_max_children && PHP_SUCCESS=true END=$(date +%s) # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_SUMMARY Summary ━━━ # ----------------------------------------------------------------------------------------------- echo "" echo "━━━━━ $ICON_SUMMARY PHP-FPM SUMMARY ━━━━━" echo "$ICON_GEAR Config File: $PHP_CONF" echo "$ICON_PHP Max Children: $PHP_MAX_CHILDREN" echo "$ICON_TIME Duration: $(format_duration $((END - START)))" if [[ "$DRY_RUN" == true ]]; then echo "$ICON_WARN Status: DRY RUN — no changes made" elif [[ "$PHP_SUCCESS" == true ]]; then echo "$ICON_DONE Status: $ICON_SUCCESS DONE" notify "PHP-FPM max_children set to $PHP_MAX_CHILDREN on $(hostname)" "PHP-FPM" "normal" else echo "$ICON_ERROR Status: $ICON_ERROR FAILED" notify "PHP-FPM config update failed on $(hostname)" "PHP-FPM" "warning" fi echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" [[ "$PHP_SUCCESS" == false ]] && exit 1 exit 0