From e3458630bac3306aabc51c0b2794bc2ee3442415 Mon Sep 17 00:00:00 2001 From: Adam Outler Date: Sat, 22 Nov 2025 01:29:50 +0000 Subject: [PATCH] Convert from crond to supercronic --- .devcontainer/Dockerfile | 9 +++++---- .devcontainer/scripts/setup.sh | 2 +- Dockerfile | 9 +++++---- Dockerfile.debian | 2 +- back/cron_script.sh | 17 +++++++++++------ front/php/components/logs_defaults.json | 6 +++--- front/php/server/util.php | 2 +- .../production-filesystem/build/init-cron.sh | 5 +++++ .../production-filesystem/build/init-crond.sh | 4 ---- install/production-filesystem/entrypoint.sh | 2 +- .../config/{crond/netalertx => cron/crontab} | 2 +- .../services/scripts/cron_script.sh | 6 +++--- .../services/{start-crond.sh => start-cron.sh} | 13 +++++++++---- 13 files changed, 46 insertions(+), 33 deletions(-) create mode 100644 install/production-filesystem/build/init-cron.sh delete mode 100755 install/production-filesystem/build/init-crond.sh rename install/production-filesystem/services/config/{crond/netalertx => cron/crontab} (67%) mode change 100755 => 100644 rename install/production-filesystem/services/{start-crond.sh => start-cron.sh} (53%) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 137e8c8a..aedd4f6b 100755 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -64,6 +64,7 @@ ENV LOG_IP_CHANGES=${NETALERTX_LOG}/IP_changes.log ENV LOG_APP=${NETALERTX_LOG}/app.log ENV LOG_APP_FRONT=${NETALERTX_LOG}/app_front.log ENV LOG_REPORT_OUTPUT_TXT=${NETALERTX_LOG}/report_output.txt +ENV LOG_CRON=${NETALERTX_LOG}/supercronic ENV LOG_DB_IS_LOCKED=${NETALERTX_LOG}/db_is_locked.log ENV LOG_REPORT_OUTPUT_HTML=${NETALERTX_LOG}/report_output.html ENV LOG_STDERR=${NETALERTX_LOG}/stderr.log @@ -71,7 +72,7 @@ ENV LOG_APP_PHP_ERRORS=${NETALERTX_LOG}/app.php_errors.log ENV LOG_EXECUTION_QUEUE=${NETALERTX_LOG}/execution_queue.log ENV LOG_REPORT_OUTPUT_JSON=${NETALERTX_LOG}/report_output.json ENV LOG_STDOUT=${NETALERTX_LOG}/stdout.log -ENV LOG_CROND=${NETALERTX_LOG}/crond.log +ENV LOG_CRON=${NETALERTX_LOG}/cron.log ENV LOG_NGINX_ERROR=${NETALERTX_LOG}/nginx-error.log # System Services configuration files @@ -81,11 +82,11 @@ ENV SYSTEM_SERVICES_SCRIPTS=${SYSTEM_SERVICES}/scripts ENV SYSTEM_SERVICES_CONFIG=${SYSTEM_SERVICES}/config ENV SYSTEM_NGINX_CONFIG=${SYSTEM_SERVICES_CONFIG}/nginx ENV SYSTEM_NGINX_CONFIG_TEMPLATE=${SYSTEM_NGINX_CONFIG}/netalertx.conf.template +ENV SYSTEM_SERVICES_CONFIG_CRON=${SYSTEM_SERVICES_CONFIG}/cron ENV SYSTEM_SERVICES_ACTIVE_CONFIG=/tmp/nginx/active-config ENV SYSTEM_SERVICES_ACTIVE_CONFIG_FILE=${SYSTEM_SERVICES_ACTIVE_CONFIG}/nginx.conf ENV SYSTEM_SERVICES_PHP_FOLDER=${SYSTEM_SERVICES_CONFIG}/php ENV SYSTEM_SERVICES_PHP_FPM_D=${SYSTEM_SERVICES_PHP_FOLDER}/php-fpm.d -ENV SYSTEM_SERVICES_CROND=${SYSTEM_SERVICES_CONFIG}/crond ENV SYSTEM_SERVICES_RUN=/tmp/run ENV SYSTEM_SERVICES_RUN_TMP=${SYSTEM_SERVICES_RUN}/tmp ENV SYSTEM_SERVICES_RUN_LOG=${SYSTEM_SERVICES_RUN}/logs @@ -119,7 +120,7 @@ ENV LANG=C.UTF-8 RUN apk add --no-cache bash mtr libbsd zip lsblk tzdata curl arp-scan iproute2 iproute2-ss nmap \ nmap-scripts traceroute nbtscan net-tools net-snmp-tools bind-tools awake ca-certificates \ sqlite php83 php83-fpm php83-cgi php83-curl php83-sqlite3 php83-session python3 envsubst \ - nginx shadow && \ + nginx supercronic shadow && \ rm -Rf /var/cache/apk/* && \ rm -Rf /etc/nginx && \ addgroup -g 20211 ${NETALERTX_GROUP} && \ @@ -165,7 +166,7 @@ RUN if [ -f .VERSION ]; then \ setcap cap_net_raw,cap_net_admin+eip $(readlink -f ${VIRTUAL_ENV_BIN}/python) && \ /bin/sh /build/init-nginx.sh && \ /bin/sh /build/init-php-fpm.sh && \ - /bin/sh /build/init-crond.sh && \ + /bin/sh /build/init-cron.sh && \ /bin/sh /build/init-backend.sh && \ rm -rf /build && \ apk del libcap && \ diff --git a/.devcontainer/scripts/setup.sh b/.devcontainer/scripts/setup.sh index a4190606..2116b0cb 100755 --- a/.devcontainer/scripts/setup.sh +++ b/.devcontainer/scripts/setup.sh @@ -26,7 +26,7 @@ LOG_FILES=( LOG_EXECUTION_QUEUE LOG_APP_PHP_ERRORS LOG_IP_CHANGES - LOG_CROND + LOG_CRON LOG_REPORT_OUTPUT_TXT LOG_REPORT_OUTPUT_HTML LOG_REPORT_OUTPUT_JSON diff --git a/Dockerfile b/Dockerfile index ca08b4c2..9093cdc1 100755 --- a/Dockerfile +++ b/Dockerfile @@ -61,6 +61,7 @@ ENV LOG_IP_CHANGES=${NETALERTX_LOG}/IP_changes.log ENV LOG_APP=${NETALERTX_LOG}/app.log ENV LOG_APP_FRONT=${NETALERTX_LOG}/app_front.log ENV LOG_REPORT_OUTPUT_TXT=${NETALERTX_LOG}/report_output.txt +ENV LOG_CRON=${NETALERTX_LOG}/supercronic ENV LOG_DB_IS_LOCKED=${NETALERTX_LOG}/db_is_locked.log ENV LOG_REPORT_OUTPUT_HTML=${NETALERTX_LOG}/report_output.html ENV LOG_STDERR=${NETALERTX_LOG}/stderr.log @@ -68,7 +69,7 @@ ENV LOG_APP_PHP_ERRORS=${NETALERTX_LOG}/app.php_errors.log ENV LOG_EXECUTION_QUEUE=${NETALERTX_LOG}/execution_queue.log ENV LOG_REPORT_OUTPUT_JSON=${NETALERTX_LOG}/report_output.json ENV LOG_STDOUT=${NETALERTX_LOG}/stdout.log -ENV LOG_CROND=${NETALERTX_LOG}/crond.log +ENV LOG_CRON=${NETALERTX_LOG}/cron.log ENV LOG_NGINX_ERROR=${NETALERTX_LOG}/nginx-error.log # System Services configuration files @@ -78,11 +79,11 @@ ENV SYSTEM_SERVICES_SCRIPTS=${SYSTEM_SERVICES}/scripts ENV SYSTEM_SERVICES_CONFIG=${SYSTEM_SERVICES}/config ENV SYSTEM_NGINX_CONFIG=${SYSTEM_SERVICES_CONFIG}/nginx ENV SYSTEM_NGINX_CONFIG_TEMPLATE=${SYSTEM_NGINX_CONFIG}/netalertx.conf.template +ENV SYSTEM_SERVICES_CONFIG_CRON=${SYSTEM_SERVICES_CONFIG}/cron ENV SYSTEM_SERVICES_ACTIVE_CONFIG=/tmp/nginx/active-config ENV SYSTEM_SERVICES_ACTIVE_CONFIG_FILE=${SYSTEM_SERVICES_ACTIVE_CONFIG}/nginx.conf ENV SYSTEM_SERVICES_PHP_FOLDER=${SYSTEM_SERVICES_CONFIG}/php ENV SYSTEM_SERVICES_PHP_FPM_D=${SYSTEM_SERVICES_PHP_FOLDER}/php-fpm.d -ENV SYSTEM_SERVICES_CROND=${SYSTEM_SERVICES_CONFIG}/crond ENV SYSTEM_SERVICES_RUN=/tmp/run ENV SYSTEM_SERVICES_RUN_TMP=${SYSTEM_SERVICES_RUN}/tmp ENV SYSTEM_SERVICES_RUN_LOG=${SYSTEM_SERVICES_RUN}/logs @@ -116,7 +117,7 @@ ENV LANG=C.UTF-8 RUN apk add --no-cache bash mtr libbsd zip lsblk tzdata curl arp-scan iproute2 iproute2-ss nmap \ nmap-scripts traceroute nbtscan net-tools net-snmp-tools bind-tools awake ca-certificates \ sqlite php83 php83-fpm php83-cgi php83-curl php83-sqlite3 php83-session python3 envsubst \ - nginx shadow && \ + nginx supercronic shadow && \ rm -Rf /var/cache/apk/* && \ rm -Rf /etc/nginx && \ addgroup -g 20211 ${NETALERTX_GROUP} && \ @@ -162,7 +163,7 @@ RUN if [ -f .VERSION ]; then \ setcap cap_net_raw,cap_net_admin+eip $(readlink -f ${VIRTUAL_ENV_BIN}/python) && \ /bin/sh /build/init-nginx.sh && \ /bin/sh /build/init-php-fpm.sh && \ - /bin/sh /build/init-crond.sh && \ + /bin/sh /build/init-cron.sh && \ /bin/sh /build/init-backend.sh && \ rm -rf /build && \ apk del libcap && \ diff --git a/Dockerfile.debian b/Dockerfile.debian index f67f0e02..316eafe7 100755 --- a/Dockerfile.debian +++ b/Dockerfile.debian @@ -72,7 +72,7 @@ ENV LOG_APP_PHP_ERRORS=${NETALERTX_LOG}/app.php_errors.log ENV LOG_EXECUTION_QUEUE=${NETALERTX_LOG}/execution_queue.log ENV LOG_REPORT_OUTPUT_JSON=${NETALERTX_LOG}/report_output.json ENV LOG_STDOUT=${NETALERTX_LOG}/stdout.log -ENV LOG_CROND=${NETALERTX_LOG}/crond.log +ENV LOG_CRON=${NETALERTX_LOG}/cron.log ENV LOG_NGINX_ERROR=${NETALERTX_LOG}/nginx-error.log # System Services configuration files diff --git a/back/cron_script.sh b/back/cron_script.sh index 5c86d909..a3d65e2a 100755 --- a/back/cron_script.sh +++ b/back/cron_script.sh @@ -3,12 +3,17 @@ export INSTALL_DIR=/app LOG_FILE="${INSTALL_DIR}/log/execution_queue.log" -# Check if there are any entries with cron_restart_backend -if grep -q "cron_restart_backend" "$LOG_FILE"; then - # Restart python application using s6 - s6-svc -r /var/run/s6-rc/servicedirs/netalertx - echo 'done' +if [ -f "${LOG_EXECUTION_QUEUE}" ] && grep -q "cron_restart_backend" "${LOG_EXECUTION_QUEUE}"; then + echo "$(date): Restarting backend triggered by cron_restart_backend" + killall python3 || echo "killall python3 failed or no process found" + sleep 2 + /services/start-backend.sh & # Remove all lines containing cron_restart_backend from the log file - sed -i '/cron_restart_backend/d' "$LOG_FILE" + # Atomic replacement with temp file. grep returns 1 if no lines selected (file becomes empty), which is valid here. + grep -v "cron_restart_backend" "${LOG_EXECUTION_QUEUE}" > "${LOG_EXECUTION_QUEUE}.tmp" + RC=$? + if [ $RC -eq 0 ] || [ $RC -eq 1 ]; then + mv "${LOG_EXECUTION_QUEUE}.tmp" "${LOG_EXECUTION_QUEUE}" + fi fi diff --git a/front/php/components/logs_defaults.json b/front/php/components/logs_defaults.json index 491d3708..b40f2bb2 100755 --- a/front/php/components/logs_defaults.json +++ b/front/php/components/logs_defaults.json @@ -107,11 +107,11 @@ "buttons": [ { "labelStringCode": "Maint_PurgeLog", - "event": "logManage('crond.log', 'cleanLog')" + "event": "logManage('cron.log', 'cleanLog')" } ], - "fileName": "crond.log", - "filePath": "__NETALERTX_LOG__/crond.log", + "fileName": "cron.log", + "filePath": "__NETALERTX_LOG__/cron.log", "textAreaCssClass": "logs logs-small" } ] \ No newline at end of file diff --git a/front/php/server/util.php b/front/php/server/util.php index 3a1adb88..e00c7086 100755 --- a/front/php/server/util.php +++ b/front/php/server/util.php @@ -274,7 +274,7 @@ function cleanLog($logFile) $path = ""; - $allowedFiles = ['app.log', 'app_front.log', 'IP_changes.log', 'stdout.log', 'stderr.log', 'app.php_errors.log', 'execution_queue.log', 'db_is_locked.log', 'nginx-error.log', 'crond.log']; + $allowedFiles = ['app.log', 'app_front.log', 'IP_changes.log', 'stdout.log', 'stderr.log', 'app.php_errors.log', 'execution_queue.log', 'db_is_locked.log', 'nginx-error.log', 'cron.log']; if(in_array($logFile, $allowedFiles)) { diff --git a/install/production-filesystem/build/init-cron.sh b/install/production-filesystem/build/init-cron.sh new file mode 100644 index 00000000..dc2770c1 --- /dev/null +++ b/install/production-filesystem/build/init-cron.sh @@ -0,0 +1,5 @@ + +#!/bin/bash +echo "Initializing cron..." + # Placeholder for cron initialization commands +echo "cron initialized." diff --git a/install/production-filesystem/build/init-crond.sh b/install/production-filesystem/build/init-crond.sh deleted file mode 100755 index af464d3e..00000000 --- a/install/production-filesystem/build/init-crond.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -echo "Initializing crond..." -#Future crond initializations can go here. -echo "crond initialized." diff --git a/install/production-filesystem/entrypoint.sh b/install/production-filesystem/entrypoint.sh index 0faa5999..87af9dc7 100755 --- a/install/production-filesystem/entrypoint.sh +++ b/install/production-filesystem/entrypoint.sh @@ -274,7 +274,7 @@ trap on_signal INT TERM # Only start crond scheduler on Alpine (non-Debian) environments # Debian typically uses systemd or other schedulers if [ "${ENVIRONMENT:-}" ] && [ "${ENVIRONMENT:-}" != "debian" ]; then - add_service "/services/start-crond.sh" "crond" + add_service "/services/start-cron.sh" "supercronic" fi # Start core frontend and backend services diff --git a/install/production-filesystem/services/config/crond/netalertx b/install/production-filesystem/services/config/cron/crontab old mode 100755 new mode 100644 similarity index 67% rename from install/production-filesystem/services/config/crond/netalertx rename to install/production-filesystem/services/config/cron/crontab index d532bac0..9bf3c9bf --- a/install/production-filesystem/services/config/crond/netalertx +++ b/install/production-filesystem/services/config/cron/crontab @@ -1,4 +1,4 @@ # Every minute check for cron jobs * * * * * /services/scripts/cron_script.sh # Update vendors 4x/d -0 */6 * * * /services/scripts/update_vendors.sh +0 */6 * * * /services/scripts/update_vendors.sh \ No newline at end of file diff --git a/install/production-filesystem/services/scripts/cron_script.sh b/install/production-filesystem/services/scripts/cron_script.sh index 347f1a20..2d91f4b9 100755 --- a/install/production-filesystem/services/scripts/cron_script.sh +++ b/install/production-filesystem/services/scripts/cron_script.sh @@ -7,10 +7,10 @@ export INSTALL_DIR=/app if grep -q "cron_restart_backend" "${LOG_EXECUTION_QUEUE}"; then killall python3 sleep 2 - /services/start-backend.sh & + /services/start-backend.sh >/dev/null 2>&1 & # Remove all lines containing cron_restart_backend from the log file # Atomic replacement with temp file - grep -v "cron_restart_backend" "${LOG_EXECUTION_QUEUE}" > "${LOG_EXECUTION_QUEUE}.tmp" && \ - mv "${LOG_EXECUTION_QUEUE}.tmp" "${LOG_EXECUTION_QUEUE}" + grep -v "cron_restart_backend" "${LOG_EXECUTION_QUEUE}" > "${LOG_EXECUTION_QUEUE}.tmp" + mv "${LOG_EXECUTION_QUEUE}.tmp" "${LOG_EXECUTION_QUEUE}" fi diff --git a/install/production-filesystem/services/start-crond.sh b/install/production-filesystem/services/start-cron.sh similarity index 53% rename from install/production-filesystem/services/start-crond.sh rename to install/production-filesystem/services/start-cron.sh index c6e9ea70..199a0ca1 100755 --- a/install/production-filesystem/services/start-crond.sh +++ b/install/production-filesystem/services/start-cron.sh @@ -6,7 +6,7 @@ crond_pid="" cleanup() { status=$? - echo "Crond stopped! (exit ${status})" + echo "Supercronic stopped! (exit ${status})" } forward_signal() { @@ -23,11 +23,16 @@ done trap cleanup EXIT trap forward_signal INT TERM -echo "Starting /usr/sbin/crond -c \"${SYSTEM_SERVICES_CROND}\" -f -L \"${LOG_CROND}\" >>\"${LOG_CROND}\" 2>&1 &" +CRON_OPTS="--quiet" +if [ "${NETALERTX_DEBUG:-0}" -eq 1 ]; then + CRON_OPTS="--debug" +fi -/usr/sbin/crond -c "${SYSTEM_SERVICES_CROND}" -f -L "${LOG_CROND}" >>"${LOG_CROND}" 2>&1 & +echo "Starting supercronic ${CRON_OPTS} \"${SYSTEM_SERVICES_CONFIG_CRON}/crontab\" >>\"${LOG_CRON}\" 2>&1 &" + +supercronic ${CRON_OPTS} "${SYSTEM_SERVICES_CONFIG_CRON}/crontab" >>"${LOG_CRON}" 2>&1 & crond_pid=$! wait "${crond_pid}"; status=$? echo -ne " done" -exit ${status} +exit ${status} \ No newline at end of file