Search code examples
scalaunixplayframeworktypesafe-activator

Restart Play Framework Activator using a script on mac & linux


I am trying to develop a script that can restart an activator instance running on a specified port. I normally run my activator project at port 15000 and I am aiming to have it restarted using the script. I can then later call that script from a web page to have activator restarted remotely etc.

So far I found a really handy utility in Linux called fuser which can find a process listening at a specified port and kill it. Something like:

fuser -k 15000/tcp which works fine on linux but NOT on a mac.

I guess I would also need to somehow track the activator project location to start it later.

Please let me know your suggestions and comments on how this can be done.


Solution

  • I'm using a bash file for this. It works on Linux and Mac OS.

    It's named loader.sh and put into your distributions root.

    To stop it uses the kill command and the PID stored in RUNNING_PID.

    #!/bin/bash
    
    # Change IP address and port here
    address="127.0.0.1"
    port="9000"
    
    # Get directory and add it to PATH
    dir="$( cd "$( dirname "$0" )" && pwd )"
    export PATH="$dir:$dir/bin:$PATH"
    
    function start() {
        # Check if we started already
        [ -f $dir/RUNNING_PID ] && return
    
        echo -n "Starting"
    
        # You can specify a config file with -Dconfig.resource
        # or a secret with -Dplay.crypto.secret
        myApp -Dhttp.port=$port -Dhttp.address=$address > /dev/null &
    
        echo "...started"
    }
    
    function stop() {
        [ -f $dir/RUNNING_PID ] || return
        echo -n "Stopping"
        kill -SIGTERM $(cat $dir/RUNNING_PID)
        while [ -f $dir/RUNNING_PID ]
        do
            sleep 0.5
        done
    
        echo "...stopped"
    }
    
    case "$1" in
        start)
            start
            ;;
        stop)
            stop
            ;;
        restart)
            stop
            start
            ;;
        *)
            echo "Usage: loader.sh start|stop|restart"
            exit 1
            ;;
    esac