Skip to content

Instantly share code, notes, and snippets.

@ostinelli
Last active September 15, 2019 12:18
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ostinelli/b87aaefc1cb75479ca69 to your computer and use it in GitHub Desktop.
Save ostinelli/b87aaefc1cb75479ca69 to your computer and use it in GitHub Desktop.
Erlang Release init.d script

Erlang Release init.d script

If you have packaged your application into a release generated with Rebar, you might want to have the following:

  • The release started on system boot.
  • The VM monitored and restarted it if it crashes.

Use HEART

HEART is the Heartbeat Monitoring of an Erlang Runtime System. The purpose of the heart port program is to check that the Erlang runtime system it is supervising is still running, so that if the VM crashes or becomes unresponsive, it is restarted.

To do so, you just have to ensure that the -heart option is uncommented in your vm.args file.

init.d script

Create the file /etc/init.d/myapp:

$ sudo vim /etc/init.d/myapp

Paste the following:

#!/usr/bin/env bash
# myapp daemon
# chkconfig: 345 20 80
# description: myapp daemon
# processname: myapp

NAME=myapp
PROJECT_ROOT_PATH=/usr/local/$NAME
APP_SCRIPT="bin/$NAME"

# export
export HOME=/root

case "$1" in
start)
    printf "%-50s" "Starting $NAME..."

    # start
    cd $PROJECT_ROOT_PATH
    $APP_SCRIPT start > /dev/null 2>&1;

    # wait for pid
    for (( i=0; i<10; ++i )); do
        OUT=`$APP_SCRIPT getpid`;
        if [ $? == 0 ]; then PID=$OUT; break; fi
        sleep 1;
    done

    if [ -z "$PID" ]; then
        printf "%s\n" "Failsd"
    else
        printf "%s\n" "Ok"
    fi
;;
status)
    printf "%-50s" "Checking $NAME..."

    # wait for pid
    cd $PROJECT_ROOT_PATH
    $APP_SCRIPT getpid > /dev/null 2>&1;

    if [ $? != 0 ]; then
        printf "%s\n" "Node is not running!"
    else
        printf "%s\n" "Ok"
    fi
;;
stop)
    printf "%-50s" "Stopping $NAME..."

    # cd and stop
    cd $PROJECT_ROOT_PATH
    $APP_SCRIPT stop > /dev/null 2>&1;

    if [ $? != 0 ]; then
        printf "%s\n" "Node is not running!"
    else
        printf "%s\n" "Ok"
    fi
;;

restart)
    $0 stop
    $0 start
;;

*)
    echo "Usage: $0 {status|start|stop|restart}"
    exit 1
esac

Side note: you can see that the script waits to exit the start function until the PID is retrieved from the VM. This is not strictly necessary, although in this way you can consider dumping it into PID files or perform other types of monitoring actions besides using HEART (monit, god, ...)

After saving, ensure that this file is executable:

$ sudo update-rc.d myapp defaults

You now have a new service:

$ sudo service myapp start
Starting myapp...                                Ok

Run at boot

Ensure that the service is started at boot time:

$ sudo update-rc.d myapp defaults
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment