Commit 7c51589c authored by nextime's avatar nextime

Refactor init scripts to start daemons directly before watchdog

- Init scripts now start wssshd/wssshc daemons directly
- Wait for daemons to be fully running before starting watchdog
- Watchdog only monitors existing daemons, doesn't start them
- Watchdog exits if daemon restart fails
- Proper startup sequence: daemon → verify running → watchdog
- Fixed both wssshd and wssshc init scripts and watchdogs
parent fa858590
......@@ -242,6 +242,9 @@ main() {
sleep 3
else
log_message "Failed to restart $DAEMON_NAME"
# If daemon fails to start, exit watchdog
log_message "Watchdog exiting due to daemon restart failure"
break
fi
fi
......@@ -262,8 +265,8 @@ case "$1" in
fi
echo "Calling main() function..."
# Properly daemonize the main() function
setsid bash -c 'main' &
# Run main() function in background
main &
# Give it a moment to start
sleep 1
......
......@@ -104,16 +104,11 @@ is_running() {
start() {
echo -n "Starting $NAME: "
# Check if watchdog is already running
if [ -f "$WATCHDOG_PIDFILE" ]; then
local watchdog_pid=$(cat "$WATCHDOG_PIDFILE")
if kill -0 "$watchdog_pid" 2>/dev/null; then
echo "already running (watchdog PID: $watchdog_pid)"
# Check if daemon is already running
if is_running; then
local pid=$(cat "$PIDFILE")
echo "already running (PID: $pid)"
return 0
else
# Stale watchdog PID file
rm -f "$WATCHDOG_PIDFILE"
fi
fi
# Check if START is enabled
......@@ -141,37 +136,55 @@ start() {
return 1
fi
# Check if watchdog script exists
if [ ! -x "$WATCHDOG" ]; then
echo "watchdog script $WATCHDOG not found or not executable"
return 1
fi
# Create necessary directories
mkdir -p /var/run /var/log/wssshc
chown $USER:$GROUP /var/run /var/log/wssshc 2>/dev/null || true
# Start the watchdog (which will start and monitor the daemon)
$WATCHDOG start >/dev/null 2>&1
local retval=$?
# Start the daemon directly
echo "Starting daemon..."
if "$DAEMON" &
then
echo $! > "$PIDFILE"
echo "Daemon started successfully"
else
echo "FAILED"
echo "Could not start daemon"
return 1
fi
if [ $retval -eq 0 ]; then
# Wait a moment for the watchdog to start (reduced since watchdog now waits for PID file)
# Wait for daemon to be fully running
local count=0
while [ $count -lt 10 ] && ! is_running; do
echo "Waiting for daemon to start... ($count/10)"
sleep 1
count=$((count + 1))
done
if is_running; then
local pid=$(cat "$PIDFILE")
echo "Daemon is running (PID: $pid)"
# Now start the watchdog to monitor the running daemon
echo "Starting watchdog..."
if [ -x "$WATCHDOG" ]; then
$WATCHDOG start >/dev/null 2>&1
sleep 2
# Check if watchdog is running
if [ -f "$WATCHDOG_PIDFILE" ] && kill -0 "$(cat "$WATCHDOG_PIDFILE")" 2>/dev/null; then
echo "OK"
return 0
else
echo "FAILED"
echo "Watchdog failed to start properly"
return 1
echo "OK (daemon running, watchdog failed)"
return 0
fi
else
echo "OK (daemon running, no watchdog)"
return 0
fi
else
echo "FAILED"
return $retval
echo "Daemon failed to start properly"
return 1
fi
}
......
......@@ -57,16 +57,11 @@ is_running() {
start() {
echo -n "Starting $NAME: "
# Check if watchdog is already running
if [ -f "$WATCHDOG_PIDFILE" ]; then
local watchdog_pid=$(cat "$WATCHDOG_PIDFILE")
if kill -0 "$watchdog_pid" 2>/dev/null; then
echo "already running (watchdog PID: $watchdog_pid)"
# Check if daemon is already running
if is_running; then
local pid=$(cat "$PIDFILE")
echo "already running (PID: $pid)"
return 0
else
# Stale watchdog PID file
rm -f "$WATCHDOG_PIDFILE"
fi
fi
# Check if config file exists
......@@ -84,37 +79,62 @@ start() {
return 1
fi
# Check if watchdog script exists
if [ ! -x "$WATCHDOG" ]; then
echo "watchdog script $WATCHDOG not found or not executable"
return 1
fi
# Create necessary directories
mkdir -p /var/run /var/log/wssshd
chown $USER:$GROUP /var/run /var/log/wssshd 2>/dev/null || true
# Start the watchdog (which will start and monitor the daemon)
$WATCHDOG start >/dev/null 2>&1
local retval=$?
# Start the daemon directly
echo "Starting daemon..."
if start-stop-daemon --start --quiet --pidfile "$PIDFILE" \
--chuid $USER:$GROUP --background --make-pidfile \
--exec "$DAEMON" 2>/dev/null; then
echo "Daemon started successfully"
else
echo "start-stop-daemon failed, trying direct execution..."
if "$DAEMON" &
then
echo $! > "$PIDFILE"
echo "Daemon started successfully (direct execution)"
else
echo "FAILED"
echo "Could not start daemon"
return 1
fi
fi
if [ $retval -eq 0 ]; then
# Wait a moment for the watchdog to start (reduced since watchdog now waits for PID file)
# Wait for daemon to be fully running
local count=0
while [ $count -lt 10 ] && ! is_running; do
echo "Waiting for daemon to start... ($count/10)"
sleep 1
count=$((count + 1))
done
if is_running; then
local pid=$(cat "$PIDFILE")
echo "Daemon is running (PID: $pid)"
# Now start the watchdog to monitor the running daemon
echo "Starting watchdog..."
if [ -x "$WATCHDOG" ]; then
$WATCHDOG start >/dev/null 2>&1
sleep 2
# Check if watchdog is running
if [ -f "$WATCHDOG_PIDFILE" ] && kill -0 "$(cat "$WATCHDOG_PIDFILE")" 2>/dev/null; then
echo "OK"
return 0
else
echo "FAILED"
echo "Watchdog failed to start properly"
return 1
echo "OK (daemon running, watchdog failed)"
return 0
fi
else
echo "OK (daemon running, no watchdog)"
return 0
fi
else
echo "FAILED"
return $retval
echo "Daemon failed to start properly"
return 1
fi
}
......
......@@ -237,6 +237,9 @@ main() {
sleep 3
else
log_message "Failed to restart $DAEMON_NAME"
# If daemon fails to start, exit watchdog
log_message "Watchdog exiting due to daemon restart failure"
break
fi
fi
......@@ -257,8 +260,8 @@ case "$1" in
fi
echo "Calling main() function..."
# Properly daemonize the main() function
setsid bash -c 'main' &
# Run main() function in background
main &
# Give it a moment to start
sleep 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment