Starting/Stopping Custom PHP Chat Server Linux Service (CentOS)

Posted by chad on Server Fault See other posts from Server Fault or by chad
Published on 2012-11-16T12:35:20Z Indexed on 2012/11/17 5:03 UTC
Read the original article Hit count: 397

Filed under:
|
|
|
|

I have been trying all night to get this service working properly. I created this script from a template and am very new to bash coding. I wrote a fully functioning chat server in php which runs endlessly, but now want to make it a dedicated service. I want to do this so that it starts on server boot and boots back up if possible when there are any down-times with the server. The issue is that I need this thing to run in a detached screen so that I can monitor packet data or send server commands via SSH when need-be.

The main problem that i'm having is that it needs to have its own PID when it starts so that I can stop/restart it when needed. I am the type who grinds on coding until I figure it out, but this is so new to me that it seems the learning curve here is very steep and frustrating. Below is my code if anybody can please help me with this one, i've gotten so tired I can't even concentrate any more :(

#!/bin/sh
#
# chatserver 
#
# chkconfig:  345 20 90
# description: chatServer Linux Service Daemon \
#              for general server handling

### BEGIN INIT INFO
# Provides: chatserver
# Required-Start: $local_fs $network $named $syslog
# Required-Stop: $local_fs $syslog
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: This service maintains the chatServer
# Description: chatServer Linux Service Daemon 
#   for general server handling
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

exec="screen php -q /var/www/html/chatServer.php"
prog="chatserver"
config="/etc/sysconfig/$prog"
pidfile="/var/run/chatserver.pid"

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/$prog

start() {
    #$exec || exit 5
    echo -n $"Starting $prog: "
    daemon $exec --name=$exec --pidfile=$pidfile
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p $pidfile
    rm -f $pidfile
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    restart
}

force_reload() {
    restart
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?

© Server Fault or respective owner

Related posts about php

Related posts about centos