diff --git a/deployment/linux/furt.service b/deployment/linux/furt.service index f09104b..123b14c 100644 --- a/deployment/linux/furt.service +++ b/deployment/linux/furt.service @@ -6,7 +6,8 @@ After=network.target Type=forking User=furt Group=furt -ExecStart=/usr/local/share/furt/scripts/start.sh start +ExecStart=/usr/local/share/furt/scripts/start.sh +PIDFile=/var/run/furt.pid WorkingDirectory=/usr/local/share/furt Restart=always RestartSec=5 diff --git a/deployment/openbsd/rc.d-furt b/deployment/openbsd/rc.d-furt index 465af19..8a5bc50 100644 --- a/deployment/openbsd/rc.d-furt +++ b/deployment/openbsd/rc.d-furt @@ -3,11 +3,52 @@ daemon="/usr/local/share/furt/scripts/start.sh" daemon_user="_furt" daemon_cwd="/usr/local/share/furt" -daemon_flags="start" . /etc/rc.d/rc.subr -pexp="lua.*src/main.lua" +# PID-File location +pidfile="/var/run/furt.pid" + +# Custom rc_check function (PID-File based) +rc_check() { + [ -f "$pidfile" ] && kill -0 $(cat "$pidfile") 2>/dev/null +} + +# Custom rc_stop function (PID-File based) +rc_stop() { + if [ -f "$pidfile" ]; then + local _pid=$(cat "$pidfile") + echo "Stopping furt (PID: $_pid)" + kill "$_pid" 2>/dev/null + # Wait for process to die + local _timeout=10 + while [ $_timeout -gt 0 ] && kill -0 "$_pid" 2>/dev/null; do + sleep 1 + _timeout=$((_timeout - 1)) + done + # Force kill if still running + if kill -0 "$_pid" 2>/dev/null; then + echo "Force killing furt (PID: $_pid)" + kill -9 "$_pid" 2>/dev/null + fi + rm -f "$pidfile" + echo "furt stopped" + else + echo "furt not running (no PID-File)" + fi +} + +# Custom rc_reload function (signal-based) +rc_reload() { + if rc_check; then + local _pid=$(cat "$pidfile") + echo "Reloading furt configuration (PID: $_pid)" + kill -HUP "$_pid" + else + echo "furt not running" + return 1 + fi +} rc_cmd $1 diff --git a/scripts/start.sh b/scripts/start.sh index 4ad5591..69b117b 100755 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -19,8 +19,10 @@ LUA_COMMAND="" # Config check first if [ "$(uname)" = "OpenBSD" ] || [ "$(uname)" = "FreeBSD" ]; then CONFIG_FILE="/usr/local/etc/furt/furt.conf" + PID_FILE="/var/run/furt.pid" else CONFIG_FILE="/etc/furt/furt.conf" + PID_FILE="/var/run/furt.pid" fi if [ ! -f "$CONFIG_FILE" ] && [ ! -f "$PROJECT_DIR/config/furt.conf" ]; then @@ -87,12 +89,38 @@ cd "$PROJECT_DIR" echo -e "${GREEN}Starting Furt...${NC}" +# PID-File cleanup function +cleanup_pid() { + if [ -f "$PID_FILE" ]; then + rm -f "$PID_FILE" + fi +} + # Service vs Interactive Detection if [ ! -t 0 ] || [ ! -t 1 ]; then - # Service mode - Background + # Service mode - Background + PID-File + echo -e "${GREEN}Service mode: Background + PID-File${NC}" + + # Start process in background "$LUA_COMMAND" src/main.lua & + PID=$! + + # Write PID-File + echo "$PID" > "$PID_FILE" + echo -e "${GREEN}Furt started (PID: $PID, PID-File: $PID_FILE)${NC}" + + # Verify process is still running after short delay + sleep 1 + if ! kill -0 "$PID" 2>/dev/null; then + echo -e "${RED}Error: Process died immediately${NC}" + cleanup_pid + exit 1 + fi + + echo -e "${GREEN}Service startup successful${NC}" else - # Interactive mode - Foreground + # Interactive mode - Foreground (no PID-File) + echo -e "${GREEN}Interactive mode: Foreground${NC}" exec "$LUA_COMMAND" src/main.lua fi