diff --git a/agent-node/VERSION b/agent-node/VERSION index b0f3d96..66c4c22 100644 --- a/agent-node/VERSION +++ b/agent-node/VERSION @@ -1 +1 @@ -1.0.8 +1.0.9 diff --git a/agent-node/install_service.py b/agent-node/install_service.py index ee246d2..b05c7d2 100755 --- a/agent-node/install_service.py +++ b/agent-node/install_service.py @@ -79,7 +79,83 @@ except subprocess.CalledProcessError as e: print(f"❌ Failed to load launchd service: {e}") +def _is_systemd_available(): + try: + # Check if systemd is running (PID 1) + with open("/proc/1/comm", "r") as f: + if "systemd" in f.read(): + # Even if systemd is PID 1, --user might fail if no session D-Bus + r = subprocess.run(["systemctl", "--user", "list-units"], capture_output=True) + return r.returncode == 0 + except: + pass + return False + +def install_linux_background_loop(): + print("Systemd not available or refusing connection. Falling back to background loop (nohup)...") + + # Create a small control script to manage the background process + ctl_script = os.path.join(get_working_dir(), "cortex-ctl") + + script_content = f"""#!/bin/sh +# Cortex Agent Control Script (Background-loop mode) +PIDFILE="{get_working_dir()}/agent.pid" +LOGFILE="{os.path.expanduser("~")}/.cortex/agent.out.log" + +case "$1" in + start) + if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") 2>/dev/null; then + echo "Agent is already running (PID $(cat $PIDFILE))" + exit 0 + fi + echo "Starting Cortex Agent..." + mkdir -p "$(dirname "$LOGFILE")" + nohup {get_python_path()} {get_agent_main_path()} >> "$LOGFILE" 2>&1 & + echo $! > "$PIDFILE" + echo "Agent started (PID $!)" + ;; + stop) + if [ -f "$PIDFILE" ]; then + echo "Stopping Cortex Agent (PID $(cat $PIDFILE))..." + kill $(cat "$PIDFILE") + rm "$PIDFILE" + else + echo "Agent is not running" + fi + ;; + status) + if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") 2>/dev/null; then + echo "Agent is RUNNING (PID $(cat $PIDFILE))" + else + echo "Agent is STOPPED" + fi + ;; + logs) + tail -f "$LOGFILE" + ;; + *) + echo "Usage: $0 {{start|stop|status|logs}}" + exit 1 +esac +""" + with open(ctl_script, "w") as f: + f.write(script_content) + os.chmod(ctl_script, 0o755) + + # Start it + try: + subprocess.run([ctl_script, "start"], check=True) + print("✅ Linux Background Service successfully started!") + print(f"Management script created at: {ctl_script}") + print(f"Use '{ctl_script} status' to check.") + except Exception as e: + print(f"❌ Failed to start background loop: {e}") + def install_linux_systemd(): + if not _is_systemd_available(): + install_linux_background_loop() + return + print("Installing Linux systemd user service...") service_content = f"""[Unit] @@ -127,6 +203,8 @@ print(" Logs: journalctl --user -u cortex-agent -f") except subprocess.CalledProcessError as e: print(f"❌ Failed to configure systemd service: {e}") + # Final fallback + install_linux_background_loop() def main(): if not os.path.exists(get_agent_main_path()):