From 5ebcd72160c4c9c414ae715a635c68ab3b9fd204 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Wed, 31 Dec 2025 07:00:39 +0000 Subject: [PATCH] Add systemd service support to installer - Add has_systemd_support() function to detect if systemd is available - Checks for systemctl command - Verifies /run/systemd/system directory exists - Confirms PID 1 is systemd (handles Docker/GitHub Actions) - Tests communication with systemd via show-environment - Add install_systemd_service() function to create and enable service - Creates /etc/systemd/system/openplc-runtime.service - Runs daemon-reload and enables service with --now flag - Service runs start_openplc.sh as root - Update installation completion message - Shows systemctl commands when service is installed - Falls back to manual start instructions when systemd unavailable This allows native Linux installations to automatically start the runtime on boot while gracefully skipping service installation in Docker containers and GitHub Actions environments. Co-Authored-By: Thiago Alves --- install.sh | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 106 insertions(+), 5 deletions(-) diff --git a/install.sh b/install.sh index 7f117937..fa88a5ec 100755 --- a/install.sh +++ b/install.sh @@ -61,6 +61,79 @@ log_error() { echo -e "${RED}[ERROR]${NC} $1" } +# Check if systemd is available and functional +# Returns 0 if systemd is available, 1 otherwise +has_systemd_support() { + # Skip on MSYS2/Windows + if is_msys2; then + return 1 + fi + + # Check if systemctl command exists + if ! command -v systemctl >/dev/null 2>&1; then + return 1 + fi + + # Check if systemd is running as PID 1 + # This handles Docker containers and GitHub Actions where systemctl may exist but systemd isn't PID 1 + if [ ! -d "/run/systemd/system" ]; then + return 1 + fi + + # Additional check: verify PID 1 is actually systemd + if [ -f "/proc/1/comm" ]; then + local pid1_name + pid1_name=$(cat /proc/1/comm 2>/dev/null) + if [ "$pid1_name" != "systemd" ]; then + return 1 + fi + fi + + # Final check: can we actually communicate with systemd? + # Use 'if' to prevent set -e from aborting on failure + if ! systemctl show-environment >/dev/null 2>&1; then + return 1 + fi + + return 0 +} + +# Install systemd service for OpenPLC Runtime +install_systemd_service() { + local service_file="/etc/systemd/system/openplc-runtime.service" + + log_info "Installing OpenPLC Runtime systemd service..." + + # Create the service file + cat > "$service_file" < "$OPENPLC_DIR/.installed" + # Check if systemd is available and install the service + SYSTEMD_SERVICE_INSTALLED=0 + if has_systemd_support; then + log_info "Systemd detected. Installing OpenPLC Runtime service..." + if install_systemd_service; then + SYSTEMD_SERVICE_INSTALLED=1 + else + log_warning "Failed to install systemd service. You can start the runtime manually." + fi + else + log_info "Systemd not available. Skipping service installation." + fi + + echo "" + echo "OpenPLC Runtime v4 is ready to use." + echo "" + + if [ "$SYSTEMD_SERVICE_INSTALLED" -eq 1 ]; then + echo "The OpenPLC Runtime service has been installed and started." + echo "The runtime will automatically start on system boot." + echo "" + echo "Useful commands:" + echo " sudo systemctl status openplc-runtime - Check service status" + echo " sudo systemctl stop openplc-runtime - Stop the service" + echo " sudo systemctl start openplc-runtime - Start the service" + echo " sudo systemctl restart openplc-runtime - Restart the service" + echo " sudo journalctl -u openplc-runtime -f - View service logs" + else + echo "To start the OpenPLC Runtime v4, run:" + echo "sudo ./start_openplc.sh" + fi + else echo "ERROR: Build process failed!" >&2 echo "Please check the error messages above for details." >&2