Note that this is NOT a php or ssl question.
The following script will start a sockets server. The first argument passed to server.php will specify the port to be listened to, and the second argument will specify whether standard tcp or ssl is used. I start the system upon boot using
chkconfig --levels 235 socketserver on and can start, stop, etc it using something like
/etc/init.d/socketserver start.
Now, I want to have the server listen in both modes at the same time. Furthermore, I don't wish to hardcode the port. I was thinking of just doing
/etc/init.d/socketserver start 2222 0 or
/etc/init.d/socketserver start 2223 1, and using
$2 and
$3 in the script to send to server.php. But this won't work as it will have the same
/var/run/socket_server.pid. Furthermore, I am not sure how I could pass the arguments to
chkconfig --levels 235 socketserver on.
How can this be best accomplished?
filename: /etc/init.d/socketserver
Code:
#!/bin/sh
### BEGIN INIT INFO
# Provides: Socket Server
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start Socket Server
# Description: Start Socket Server
### END INIT INFO
SCRIPT="/usr/bin/php /var/www/socket/server.php 2222 0"
//SCRIPT="/usr/bin/php /var/www/socket/server.php 2223 1"
RUNAS=Michael
PIDFILE=/var/run/socket_server.pid
LOGFILE=/var/log/socket_server.log
PROG="Socket Server"
start() {
if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE"); then
echo 'Service already running' >&2
return 1
fi
echo 'Starting service…' >&2
#redirct I/O so non-root user doesn't need special write position to log file.
local CMD="$SCRIPT >&3 2>&1 & echo \$!"
su -c "$CMD" $RUNAS 3>"$LOGFILE" >"$PIDFILE"
echo 'Service started' >&2
}
stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo 'Service not running' >&2
return 1
fi
echo 'Stopping service…' >&2
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
echo 'Service stopped' >&2
}
status() {
if [ -f "$PIDFILE" ] && ps -p $(cat $PIDFILE) >/dev/null;
#if [ -d /proc/$(<$PIDFILE) ];
then
echo "$PROG is running"
else
echo "$PROG is not running"
fi
RETVAL=$?
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
esac