Commit 14525e6c authored by nextime's avatar nextime

Add wssshd init script for system service management

- Create comprehensive init script with start/stop/restart/status actions
- Check for /etc/wssshd.conf existence on start with helpful error message
- Redirect daemon output to syslog for proper logging
- Implement proper process management with PID file handling
- Follow LSB (Linux Standard Base) init script conventions
- Support for daemon user/group configuration
- Graceful shutdown with process termination and cleanup
- Compatible with chkconfig and systemd service management
parent 92bed400
#!/bin/bash
#
# wssshd Startup script for WebSocket SSH Daemon
#
# chkconfig: 345 85 15
# description: WebSocket SSH Daemon - Handles WebSocket connections from wsssh/wsscp clients
# processname: wssshd
# pidfile: /var/run/wssshd.pid
# config: /etc/wssshd.conf
### BEGIN INIT INFO
# Provides: wssshd
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $network $syslog
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: WebSocket SSH Daemon
# Description: WebSocket SSH Daemon handles WebSocket connections from wsssh/wsscp clients
### END INIT INFO
# Source function library
. /etc/rc.d/init.d/functions
# Configuration
NAME="wssshd"
DAEMON="/usr/bin/wssshd.py"
PIDFILE="/var/run/wssshd.pid"
CONFIG="/etc/wssshd.conf"
LOG_FACILITY="daemon"
USER="wssshd"
GROUP="wssshd"
# Check if we're running as root
if [ $(id -u) != 0 ]; then
echo "Error: This script must be run as root"
exit 1
fi
# Function to check if process is running
is_running() {
if [ -f "$PIDFILE" ]; then
local pid=$(cat "$PIDFILE")
if [ -d "/proc/$pid" ]; then
return 0
else
# Stale PID file
rm -f "$PIDFILE"
return 1
fi
fi
return 1
}
# Function to start the daemon
start() {
echo -n $"Starting $NAME: "
# Check if already running
if is_running; then
echo -n "already running"
echo_success
echo
return 0
fi
# Check if config file exists
if [ ! -f "$CONFIG" ]; then
echo -n "configuration file $CONFIG not found"
echo_failure
echo
echo "Please create $CONFIG with the required settings."
echo "You can use the example configuration as a template:"
echo " cp /usr/share/wsssh/wssshd.conf.example $CONFIG"
return 1
fi
# Check if daemon executable exists
if [ ! -x "$DAEMON" ]; then
echo -n "daemon executable $DAEMON not found or not executable"
echo_failure
echo
return 1
fi
# Create PID directory if it doesn't exist
mkdir -p /var/run
chown $USER:$GROUP /var/run 2>/dev/null || true
# Start the daemon with syslog redirection
daemon --pidfile="$PIDFILE" --user="$USER" \
"exec $DAEMON --config $CONFIG 2>&1 | logger -t $NAME -p $LOG_FACILITY.info"
local retval=$?
if [ $retval -eq 0 ]; then
# Wait a moment for the daemon to start
sleep 2
# Check if it actually started
if is_running; then
echo_success
echo
return 0
else
echo_failure
echo
echo "Daemon failed to start properly"
return 1
fi
else
echo_failure
echo
return $retval
fi
}
# Function to stop the daemon
stop() {
echo -n $"Stopping $NAME: "
if ! is_running; then
echo -n "not running"
echo_success
echo
return 0
fi
# Try graceful shutdown first
if [ -f "$PIDFILE" ]; then
local pid=$(cat "$PIDFILE")
kill -TERM $pid 2>/dev/null
# Wait up to 30 seconds for graceful shutdown
local count=0
while [ $count -lt 30 ] && is_running; do
sleep 1
count=$((count + 1))
done
if is_running; then
# Force kill if graceful shutdown failed
echo -n "forcing shutdown... "
kill -KILL $pid 2>/dev/null
sleep 2
fi
fi
# Clean up PID file
rm -f "$PIDFILE"
if is_running; then
echo_failure
echo
return 1
else
echo_success
echo
return 0
fi
}
# Function to restart the daemon
restart() {
stop
sleep 2
start
}
# Function to check status
status() {
if is_running; then
local pid=$(cat "$PIDFILE")
echo "$NAME is running (PID: $pid)"
return 0
else
echo "$NAME is not running"
return 3
fi
}
# Function to reload configuration
reload() {
echo -n $"Reloading $NAME configuration: "
if ! is_running; then
echo -n "not running"
echo_failure
echo
return 1
fi
local pid=$(cat "$PIDFILE")
kill -HUP $pid 2>/dev/null
if [ $? -eq 0 ]; then
echo_success
echo
return 0
else
echo_failure
echo
return 1
fi
}
# Main script logic
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
status)
status
;;
condrestart|try-restart)
if is_running; then
restart
fi
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status|condrestart}"
exit 2
;;
esac
exit $?
\ No newline at end of file
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