diff --git a/etc/home/supervisor.default b/etc/home/supervisor.common.conf similarity index 64% rename from etc/home/supervisor.default rename to etc/home/supervisor.common.conf index b938145a..e212090d 100644 --- a/etc/home/supervisor.default +++ b/etc/home/supervisor.common.conf @@ -15,12 +15,4 @@ serverurl=unix:///var/run/supervisor/supervisord.sock file=/var/run/supervisor/supervisord.sock ; path to your socket file [rpcinterface:supervisor] -supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface - -[program:${process_name}] -command=${command} -autostart=${autostart} -autorestart=${autorestart} -stderr_logfile=/var/log/supervisor/${process_name}.err.log -stdout_logfile=/var/log/supervisor/${process_name}.out.log -environment=${envs} +supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface \ No newline at end of file diff --git a/etc/home/supervisor.program.conf b/etc/home/supervisor.program.conf new file mode 100644 index 00000000..7b6f5373 --- /dev/null +++ b/etc/home/supervisor.program.conf @@ -0,0 +1,7 @@ +[program:${process_name}] +command=${command} +autostart=${autostart} +autorestart=${autorestart} +stderr_logfile=/var/log/supervisor/${process_name}.err.log +stdout_logfile=/var/log/supervisor/${process_name}.out.log +environment=${envs} \ No newline at end of file diff --git a/lib/process_manager.sh b/lib/process_manager.sh index cd63ac3b..88d25412 100644 --- a/lib/process_manager.sh +++ b/lib/process_manager.sh @@ -2,7 +2,8 @@ # Define paths CONFIG_FILE="/etc/worker/services.yml" -TEMPLATE_FILE="/home/${USER}/etc/supervisor.default" +COMMON_TEMPLATE_FILE="/home/${USER}/etc/supervisor.common.conf" +PROGRAM_TEMPLATE_FILE="/home/${USER}/etc/supervisor.program.conf" FINAL_CONFIG="/home/${USER}/etc/supervisord.conf" # Function to check for service configurations @@ -32,11 +33,14 @@ parse_service_info() { autorestart=$(echo "$service_json" | jq -r '.autorestart // "false"') environment=$(echo "$service_json" | jq -r '.environment // [] | join(",")') + # Add an additional newline for better separation and readability + echo -e "\n" >> "$FINAL_CONFIG" # Adds two newlines to the end of the file + sed "s|\${process_name}|$name|g; \ s|\${command}|$command|g; \ s|\${autostart}|$autostart|g; \ s|\${autorestart}|$autorestart|g; \ - s|\${envs}|$environment|g" "$TEMPLATE_FILE" >> "$FINAL_CONFIG" + s|\${envs}|$environment|g" "$PROGRAM_TEMPLATE_FILE" >> "$FINAL_CONFIG" } # Function to start Supervisor with the generated configuration @@ -52,16 +56,11 @@ configure_and_execute_services() { return 1 fi - # Copy the base Supervisor configuration. - cp "$TEMPLATE_FILE" "$FINAL_CONFIG" - - # Remove the template [program:x] section from FINAL_CONFIG - # shellcheck disable=SC2016 - sed -i '/\[program:\${process_name}\]/,/^$/d' "$FINAL_CONFIG" + # Copy the base Supervisor common configuration only once at the start. + cp "$COMMON_TEMPLATE_FILE" "$FINAL_CONFIG" # Convert enabled services to JSON and process each. local services_yaml - # Filter only services with enabled: true services_yaml=$(yq e -o=json '.services[] | select(.enabled == true)' "$CONFIG_FILE" | jq -c .) if [ -z "$services_yaml" ]; then @@ -69,16 +68,7 @@ configure_and_execute_services() { return 1 fi - # Use a temporary file to avoid subshell issues - local services_file - services_file=$(mktemp) - - echo "$services_yaml" > "$services_file" - mapfile -t services_array < "$services_file" - rm -f "$services_file" - - # Process each service in the array - for service_json in "${services_array[@]}"; do + echo "$services_yaml" | while read -r service_json; do parse_service_info "$service_json" done