Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 $?
share|improve this question
1  
i had to install a mp3 player once which would only work in a screen. I had to create a init.d script but had problems with getting "screen" to work inside this. I used this script inside my script and then it worked fine. Hope this helps. –  Sc0rian Nov 16 '12 at 12:58
    
Don't run your service in "screen". –  Michael Hampton Nov 16 '12 at 16:59
    
Then how can I run it where I can SSH back to it to read packets or send remote commands? –  chad Nov 16 '12 at 21:56
    
That's what logs are for. –  Michael Hampton Nov 17 '12 at 2:22

1 Answer 1

I found a helpful solution.. I merged my code with some of what I found at http://www.linuxquestions.org/questions/programming-9/bash-scripting-creating-a-custom-service-centos-879313/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.