Start/stop script for Red Hat etc.

Hi folks,

i was unable to find a proper System V type startup script for Servoy application server, so I wrote my own (actually, pasted together bits and pieces from other scripts).

With this script you can start and stop the application server and query if it’s running. Also you can use chkconfig to turn the service on/off. Runlevel changes should work nicely too.

One problem with the script is that there is no “nice” way of shutting down the application server from unix shell? Let’s hope it is implemented soon. I’m just killing the processes. This could be disastrous, so the server should still be shut down from the administration console if possible.

All comments are welcome. Especially I wonder if using “kill” to shut down the server really can break something…

DISCLAIMER: Use the code with your own responsibility. Please understand what you’re doing, take backups and and test everything before installing it on your production server.

Installation:

AS ROOT..

  1. save the script below as /etc/init.d/servoy

  2. edit the script to ensure all the variables are correct: SRVUSER = your system’s user name to run servoy under, SERVOY_ROOT = servoy installation directory, SERVOY_EXEC = complete path to servoy startup script (servoy_server.sh)

  3. edit the servoy_server.sh script to include user dir so the server can find the preferences file. Add “-Duser.dir=$HOME/servoy” just after the word “java”.

  4. make sure you have created a user account for servoy. Important: you shouldn’t run any other processes than servoy application server under this user id.

  5. try starting, stopping and restarting the server, like this:

# /etc/init.d/servoy start
Starting Servoy services:                                  [  OK  ]
# /etc/init.d/servoy restart
Shutting down Servoy services:                             [  OK  ]
Starting Servoy services:                                  [  OK  ]
# /etc/init.d/servoy stop
Shutting down Servoy services:                             [  OK  ]
# /etc/init.d/servoy status
application server is not running

.. and verify with your browser that the server really has started and stopped.

  1. if everything seems to work you can now say chkconfig servoy on to install servoy as a service, that starts and stops automatically during runlevel changes (reboot, etc).

Here is the script:

#! /bin/sh 
# 
# System startup info for chkconfig
# 
# chkconfig: 35 95 5 
# description: Starts and stops the Servoy application server 
# 
# System startup script for the Servoy Application Server
# Verify SRVUSER, SERVOY_ROOT and SERVOY_SERVER and place in /etc/init.d
# 
### BEGIN INIT INFO 
# Provides: servoy
# Required-Start: $syslog $network $mysql
# Required-Stop:  $syslog $network $mysql
# Default-Start:  3 5 
# Default-Stop:   0 1 2 6 
# Description:    Start Servoy application server. 
### END INIT INFO 
# 
 
# source some functions 
. /etc/rc.d/init.d/functions 
. /etc/sysconfig/network 
 
# check for networking 
[ "${NETWORKING}" = "no" ] && exit 0 
 
RETVAL=0 
 
# delay is here because it may take time to stop the server 
DELAY=2 

# user name to run servoy service - do NOT use root because we kill ALL SRVUSER procs!
SRVUSER=servoy

# where to find the executables 
SERVOY_ROOT=/home/servoy/servoy
SERVOY_SERVER=$SERVOY_ROOT/servoy_server.sh 
FUSER=/sbin/fuser 
 
# make sure they all exist 
if [ ! -d $SERVOY_ROOT ]; then echo "cannot find servoy_root path"; exit 0; fi 
if [ ! -f $SERVOY_SERVER ]; then echo "cannot find servoy_server command"; exit 0; fi 
if [ ! -f $FUSER ]; then echo "cannot find fuser command"; exit 0; fi 

start() { 
        if [ "`ps -u $SRVUSER -o pid=`" ]; then 
                echo -n "application server is already running"; echo_failure; echo; exit 0 
        fi 
        echo -n "Starting Servoy services: " 
        if [ ! -z "$SERVOY_SERVER" ]; then 
            # start the server 
            su - $SRVUSER -c $SERVOY_SERVER > /dev/null 2>&1 &
        fi 
        touch /var/lock/subsys/servoy
        sleep $DELAY 
        if [ "`ps -u $SRVUSER -o pid=`" != "" ] && [ -f /var/lock/subsys/servoy ]; then 
                echo_success 
        else 
                echo_failure 
        fi 
        echo 
        return $RETVAL 
} 
 
stop() { 
        if [ ! "`ps -u $SRVUSER -o pid=`" ]; then 
                echo -n "application server is not running"; echo_failure; echo; exit 0 
        fi 
        echo -n "Shutting down Servoy services: " 
        if [ ! -z "$SERVOY_SERVER" ]; then 
            # stop the server 

	    # first we should try a nice way of stopping the server, if possible:
	    # su - $SRVUSER -c $SERVOY_SERVER stop > /dev/null 2>&1
	    
	    # wait a few seconds and then get rude
	    # sleep 4
	    kill `ps -u $SRVUSER -o pid=`
            sleep $DELAY

            if [ "`ps -u $SRVUSER -o pid=`" ]; then
                # if still running, lets get violent
	        kill -9 `ps -u $SRVUSER -o pid=`
            fi

            RETVAL=$? 
        fi 
        rm -f /var/lock/subsys/servoy
	sleep $DELAY 
        if [ "`ps -u $SRVUSER -o pid=`" == "" ] && [ ! -f /var/lock/subsys/servoy ]; then 
                echo_success 
        else 
                echo_failure 
        fi 
        echo 
        return $RETVAL 
} 
         
status() { 
        if [ ! -z "$SERVOY_SERVER" ]; then 
            if [ -x $FUSER ]; then  
                _o=`$FUSER $SERVOY_SERVER` 
                if [ $? -eq 0 ]; then 
                    echo "application server is running" 
                    RETVAL=$? 
                else 
                    echo "application server is not running" 
                    RETVAL=$? 
                fi 
           else 
                echo "status unkown  - fuser not found" >&2 
           fi    
        else 
            echo "status unkown - servoy server not found" >&2 
        fi 
        return $RETVAL 
} 
 
restart() { 
        stop 
        start 
} 
 
case "$1" in 
        start) 
                start 
                ;; 
        stop) 
                stop 
                ;; 
        status) 
                status 
                ;; 
        restart) 
                restart 
                ;; 
        *) 
                echo "Usage: servoy {start|stop|status|restart}" 
                exit 1 
esac 
 
exit $RETVAL

Love and light,
Joonas

jonttu-:
With this script you can start and stop the application server and query if it’s running. Also you can use chkconfig to turn the service on/off. Runlevel changes should work nicely too.

VERY COOL!

With the servoy_server.sh script now working when you click restart, this is an awesome addition for those of us working on Linux.

Now all we need to do is create a perl script for integration into Webmin. Maybe someday when I have no more “business work” to do I’ll sit down and write one.

Side note: For those of you using Firebird on Linux, I have recently upgraded to 1.5 (which makes some changes in how the Firebird server is configured) It works without a hitch - at least for me it has. Let’s hear it for Open Source empowering the small business with Enterprise grade tools!

Installation instructions added to the original post :)

Love and light,
Joonas

You can restart the server commandline using a tool which can fetch URL’s for example using wget:

wget http://localhost:8080/servoy-admin/?rf=1

Similarly you can stop the server with:

wget http://localhost:8080/servoy-admin/?sf=1

You might have to change the host and the port to point to the correct location depending on your installation.

To have some logging you could consider changing the startline into:

# start the server
            su - $SRVUSER -c $SERVOY_SERVER > $SERVOY_ROOT/servoy.log 2>&1 &