Skip to content

Instantly share code, notes, and snippets.

@sumpton
Forked from jinze/forever-initd-example.sh
Last active December 18, 2015 11:49
Show Gist options
  • Select an option

  • Save sumpton/5778486 to your computer and use it in GitHub Desktop.

Select an option

Save sumpton/5778486 to your computer and use it in GitHub Desktop.

Revisions

  1. sumpton revised this gist Jun 14, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions forever-initd-example.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    #!/bin/bash
    #
    # chkconfig: - 55 45
    # init.d example for Forever & Node
    #
    # modeled after
  2. sumpton revised this gist Jun 14, 2013. 1 changed file with 59 additions and 54 deletions.
    113 changes: 59 additions & 54 deletions forever-initd-example.sh
    Original file line number Diff line number Diff line change
    @@ -34,77 +34,82 @@ AWK=awk
    SED=sed

    start() {
    echo "Starting $NAME node instance: "
    echo "Starting $NAME node instance: "

    if [ "$ID" = "" ]; then
    if [ "$FOREVER_ID" = "" ]; then

    # Create the log and pid files, making sure that the target use has access to them
    mkdir -p $PID_DIR
    chown $USER $PID_DIR
    # Create the log and pid files, making sure that the target use has access to them
    mkdir -p $PID_DIR
    chown $USER $PID_DIR

    mkdir -p $LOG_DIR
    chown $USER $LOG_DIR
    mkdir -p $LOG_DIR
    chown $USER $LOG_DIR

    touch $LOGFILE
    chown $USER $LOGFILE
    touch $LOGFILE
    chown $USER $LOGFILE

    touch $PIDFILE
    chown $USER $PIDFILE
    touch $PIDFILE
    chown $USER $PIDFILE

    # Launch the application
    daemon $FOREVER start -p $FOREVER_DIR --pidFile $PIDFILE -l $LOGFILE -a --sourceDir $INSTANCE_DIR -c $COMMAND $SOURCE_NAME
    RETVAL=$?
    else
    echo "Instance already running"
    RETVAL=0
    fi
    # Launch the application
    daemon $FOREVER start -p $FOREVER_DIR --pidFile $PIDFILE -l $LOGFILE -a --sourceDir $INSTANCE_DIR -c $COMMAND $SOURCE_NAME
    RETVAL=$?
    else
    echo "Instance already running"
    RETVAL=0
    fi
    }

    restart() {
    echo -n "Restarting $NAME node instance : "
    if [ "$ID" != "" ]; then
    $FOREVER restart -p $FOREVER_DIR $ID
    RETVAL=$?
    else
    start
    fi
    echo -n "Restarting $NAME node instance : "
    if [ "$FOREVER_ID" != "" ]; then
    $FOREVER restart -p $FOREVER_DIR $FOREVER_ID
    RETVAL=$?
    else
    start
    fi
    }

    stop() {
    echo -n "Shutting down $NAME node instance : "
    if [ "$ID" != "" ]; then
    $FOREVER stop -p $FOREVER_DIR $ID
    else
    echo "Instance is not running";
    fi
    RETVAL=$?
    echo -n "Shutting down $NAME node instance : "
    if [ "$FOREVER_ID" != "" ]; then
    $FOREVER stop -p $FOREVER_DIR $FOREVER_ID
    else
    echo "Instance is not running";
    fi
    RETVAL=$?
    }

    getForeverId() {
    local PID=$(pidofproc -p $PIDFILE)
    #$FOREVER list -p $FOREVER_DIR | $SED -e 's/\x1b\[[0-9; ]*m//g' | $AWK "\$6 && \$6 == \"$pid\" { gsub(/[\[\]]/, \"\", \$2); print \$2; }";
    $FOREVER list | grep $PID | $AWK "\$7 && \$7 == \"$PID\" { gsub(/[\[\]]/, \"\", \$2); print \$2; }"
    # local PID=$(pidofproc -p $PIDFILE)
    read PID < $PIDFILE
    #$FOREVER list -p $FOREVER_DIR | $SED -e 's/\x1b\[[0-9; ]*m//g' | $AWK "\$6 && \$6 == \"$pid\" { gsub(/[\[\]]/, \"\", \$2); print \$2; }";
    $FOREVER list | grep $PID | $AWK "\$7 && \$7 == \"$PID\" { gsub(/[\[\]]/, \"\", \$2); print \$2; }"
    }

    ID=$(getForeverId)
    #echo ID: $ID
    if [ -f $PIDFILE ]; then
    FOREVER_ID=$(getForeverId)
    #echo ID: $FOREVER_ID
    else
    FOREVER_ID=''
    fi

    case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    status)
    status -p ${PIDFILE}
    ;;
    restart)
    restart
    ;;
    *)
    echo "Usage: {start|stop|status|restart}"
    exit 1
    ;;
    start)
    start
    ;;
    stop)
    stop
    ;;
    status)
    status -p ${PIDFILE}
    ;;
    restart)
    restart
    ;;
    *)
    echo "Usage: {start|stop|status|restart}"
    exit 1
    ;;
    esac
    exit $RETVAL
    exit $RETVAL
  3. sumpton revised this gist Jun 14, 2013. 1 changed file with 49 additions and 36 deletions.
    85 changes: 49 additions & 36 deletions forever-initd-example.sh
    Original file line number Diff line number Diff line change
    @@ -1,48 +1,58 @@
    #!/bin/bash
    #
    # initd-example Node init.d
    # init.d example for Forever & Node
    #
    # chkconfig: 345 80 20
    # description: Node init.d example
    # processname: node
    # pidfile: /var/run/initd-example.pid
    # logfile: /var/log/initd-example.log
    # modeled after
    # https://gist.github.com/jinze/3748766
    #
    # supports vhost
    #
    # pidfile: /var/run/nodejs/initd-example.pid
    # logfile: /var/log/nodejs/initd-example.log

    # Source function library.
    . /lib/lsb/init-functions
    . /etc/init.d/functions

    NAME=feeds-process # Unique name for the application
    NODE_ENV=production # Node environment
    PORT=3000 # Port (in this case the application uses process.env.PORT to set the port)
    SOURCE_NAME=app.js # Name os the applcation entry point script

    NAME=initd-example # Unique name for the application
    NODE_ENV=production # Node environment
    PORT=1234 # Port (in this case the application uses process.env.PORT to set the port)
    INSTANCE_DIR=/var/www/$NAME # Location of the application source
    COMMAND=coffee # Command to run
    SOURCE_NAME=app.coffee # Name os the applcation entry point script
    USER=nodejs
    INSTANCE_DIR=/var/www/$NAME # Location of the application source
    COMMAND=node # Command to run

    user=apache
    pidfile=/var/run/$NAME.pid
    logfile=/var/log/$NAME.log
    forever_dir=/var/run/forever # Forever root directory.
    PID_DIR=/var/run/nodejs
    PIDFILE=$PID_DIR/$NAME.pid
    LOG_DIR=/var/log/nodejs
    LOGFILE=$LOG_DIR/$NAME.log
    FOREVER_DIR=/var/run/forever # Forever root directory.

    node=node
    forever=forever
    awk=awk
    sed=sed
    NODE=node
    FOREVER=/usr/bin/forever
    AWK=awk
    SED=sed

    start() {
    echo "Starting $NAME node instance: "

    if [ "$id" = "" ]; then
    if [ "$ID" = "" ]; then

    # Create the log and pid files, making sure that the target use has access to them
    touch $logfile
    chown $user $logfile
    mkdir -p $PID_DIR
    chown $USER $PID_DIR

    touch $pidfile
    chown $user $pidfile
    mkdir -p $LOG_DIR
    chown $USER $LOG_DIR

    touch $LOGFILE
    chown $USER $LOGFILE

    touch $PIDFILE
    chown $USER $PIDFILE

    # Launch the application
    start_daemon
    $forever start -p $forever_dir --pidFile $pidfile -l $logfile -a -d $INSTANCE_DIR -c $COMMAND $SOURCE_NAME
    daemon $FOREVER start -p $FOREVER_DIR --pidFile $PIDFILE -l $LOGFILE -a --sourceDir $INSTANCE_DIR -c $COMMAND $SOURCE_NAME
    RETVAL=$?
    else
    echo "Instance already running"
    @@ -52,8 +62,8 @@ start() {

    restart() {
    echo -n "Restarting $NAME node instance : "
    if [ "$id" != "" ]; then
    $forever restart -p $forever_dir $id
    if [ "$ID" != "" ]; then
    $FOREVER restart -p $FOREVER_DIR $ID
    RETVAL=$?
    else
    start
    @@ -62,19 +72,22 @@ restart() {

    stop() {
    echo -n "Shutting down $NAME node instance : "
    if [ "$id" != "" ]; then
    $forever stop -p $forever_dir $id
    if [ "$ID" != "" ]; then
    $FOREVER stop -p $FOREVER_DIR $ID
    else
    echo "Instance is not running";
    fi
    RETVAL=$?
    }

    getForeverId() {
    local pid=$(pidofproc -p $pidfile)
    $forever list -p $forever_dir | $sed -e 's/\x1b\[[0-9; ]*m//g' | $awk "\$6 && \$6 == \"$pid\" { gsub(/[\[\]]/, \"\", \$2); print \$2; }";
    local PID=$(pidofproc -p $PIDFILE)
    #$FOREVER list -p $FOREVER_DIR | $SED -e 's/\x1b\[[0-9; ]*m//g' | $AWK "\$6 && \$6 == \"$pid\" { gsub(/[\[\]]/, \"\", \$2); print \$2; }";
    $FOREVER list | grep $PID | $AWK "\$7 && \$7 == \"$PID\" { gsub(/[\[\]]/, \"\", \$2); print \$2; }"
    }
    id=$(getForeverId)

    ID=$(getForeverId)
    #echo ID: $ID

    case "$1" in
    start)
    @@ -84,7 +97,7 @@ case "$1" in
    stop
    ;;
    status)
    status -p ${pidfile}
    status -p ${PIDFILE}
    ;;
    restart)
    restart
  4. @jinze jinze created this gist Sep 19, 2012.
    97 changes: 97 additions & 0 deletions forever-initd-example.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,97 @@
    #!/bin/bash
    #
    # initd-example Node init.d
    #
    # chkconfig: 345 80 20
    # description: Node init.d example
    # processname: node
    # pidfile: /var/run/initd-example.pid
    # logfile: /var/log/initd-example.log
    #

    # Source function library.
    . /lib/lsb/init-functions

    NAME=initd-example # Unique name for the application
    NODE_ENV=production # Node environment
    PORT=1234 # Port (in this case the application uses process.env.PORT to set the port)
    INSTANCE_DIR=/var/www/$NAME # Location of the application source
    COMMAND=coffee # Command to run
    SOURCE_NAME=app.coffee # Name os the applcation entry point script

    user=apache
    pidfile=/var/run/$NAME.pid
    logfile=/var/log/$NAME.log
    forever_dir=/var/run/forever # Forever root directory.

    node=node
    forever=forever
    awk=awk
    sed=sed

    start() {
    echo "Starting $NAME node instance: "

    if [ "$id" = "" ]; then
    # Create the log and pid files, making sure that the target use has access to them
    touch $logfile
    chown $user $logfile

    touch $pidfile
    chown $user $pidfile

    # Launch the application
    start_daemon
    $forever start -p $forever_dir --pidFile $pidfile -l $logfile -a -d $INSTANCE_DIR -c $COMMAND $SOURCE_NAME
    RETVAL=$?
    else
    echo "Instance already running"
    RETVAL=0
    fi
    }

    restart() {
    echo -n "Restarting $NAME node instance : "
    if [ "$id" != "" ]; then
    $forever restart -p $forever_dir $id
    RETVAL=$?
    else
    start
    fi
    }

    stop() {
    echo -n "Shutting down $NAME node instance : "
    if [ "$id" != "" ]; then
    $forever stop -p $forever_dir $id
    else
    echo "Instance is not running";
    fi
    RETVAL=$?
    }

    getForeverId() {
    local pid=$(pidofproc -p $pidfile)
    $forever list -p $forever_dir | $sed -e 's/\x1b\[[0-9; ]*m//g' | $awk "\$6 && \$6 == \"$pid\" { gsub(/[\[\]]/, \"\", \$2); print \$2; }";
    }
    id=$(getForeverId)

    case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    status)
    status -p ${pidfile}
    ;;
    restart)
    restart
    ;;
    *)
    echo "Usage: {start|stop|status|restart}"
    exit 1
    ;;
    esac
    exit $RETVAL