3 Ubuntu init.d for Haste Server
rkl110 edited this page 2017-02-22 15:59:16 +01:00

This is a basic init.d script for haste-server running on Ubuntu. This has been tested on Ubuntu 14.04 and 16.04.

#!/bin/sh

### BEGIN INIT INFO
# Provides:          hasteserver
# Required-Start:    $local_fs $network $redis-server
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts Hastebin Server
# Description:       Starts Hastebin Server for Ubuntu 14.04 or higher
### END INIT INFO


. /lib/lsb/init-functions

# Change the value of "USER" to linux user name who runs Haste Server
    # $USER never gets used only in echo commands can not defind user to run Haste Server
USER=root;

# Change the value of "DAEMON" to the Node.js process path
DAEMON="/usr/local/bin/node";
# Change the value of "ROOT_DIR" to hasteserver installation path
ROOT_DIR="/var/www/haste";

# Only change these variables if you intentionally modified Haste Server
SERVER="$ROOT_DIR/server.js";
LOG_FILE="/var/log/hasteserver/server.js.log";

do_start()
{
		echo `date` >> $LOG_FILE;
		echo " Starting Hastebin Server ..." | tee -a $LOG_FILE;
		cd $ROOT_DIR;
		nohup $DAEMON $SERVER >> $LOG_FILE 2>&1 &
		RETVAL=$?;
		pid=`ps -aefw | grep "$DAEMON $SERVER" | grep -v " grep " | awk '{print $2}'`;

		if [ ${RETVAL} -eq 0 ];
		  then
				log_success_msg " Hastebin Server Started ($pid)" | tee -a $LOG_FILE;
		  else
				log_failure_msg " Hastebin Server failed with exit code ${RETVAL}" | tee -a $LOG_FILE;
		  fi;
}
do_stop()
{
		echo `date` >> $LOG_FILE;
		echo " Stopping Hastebin Server ..." | tee -a $LOG_FILE;
		pid=`ps -aefw | grep "$DAEMON $SERVER" | grep -v " grep " | awk '{print $2}'`;
		if [ -z "$pid" ];
		  then
			log_success_msg " Hastebin Server was not running\n" | tee -a $LOG_FILE;
		  else
			echo " Terminating PID $pid ..." | tee -a $LOG_FILE;
				kill -9 $pid >> $LOG_FILE 2>&1;
			RETVAL=$?;
			if [ ${RETVAL} -eq 0 ];
			  then
				log_success_msg " Hastebin Server has stopped\n" | tee -a $LOG_FILE;
			  else
				log_failure_msg " ERROR: Hastebin Server failed to stop - PID $pid could not be terminated\n" | tee -a $LOG_FILE;
			  fi;
		  fi;
}
do_status()
{
		pid=`ps -aefw | grep "$DAEMON $SERVER" | grep -v " grep " | awk '{print $2}'`;
		node=`node -v`;
		if [ -z "$pid" ];
		  then
			log_failure_msg " Hastebin Server is not running";
			echo " Location: $ROOT_DIR";
			echo " Node.js: $node at $DAEMON";
			echo " User: $USER";
		  else
			log_success_msg "Hastebin Server is running";
			echo " Location: $ROOT_DIR";
			echo " Node.js: $node at $DAEMON";
			echo " User: $USER";
			echo " Process ID: $pid";
			uptime=`ps -p "$pid" -o etime= | xargs`;
			echo " Uptime: $uptime";
		  fi
}

case "$1" in
		start)
				do_start
		;;
		stop)
				do_stop
		;;
		status)
				do_status
		;;
		restart)
				do_stop
				do_start
		;;
		*)
				echo "Usage: $0 {start|stop|status|restart}"
				exit 0
		;;
esac
exit

Copy and paste the script into /etc/init.d/hasteserver and then edit these variables in the script according to your installation of haste-server:

  • USER
  • DAEMON
  • ROOT_DIR
  • LOG_FILE

Make the script executable and owned by root:

sudo chmod 755 /etc/init.d/hasteserver
sudo chown root:root /etc/init.d/hasteserver

Now you should be able to use the commands:

service hasteserver start
service hasteserver stop
service hasteserver status
service hasteserver restart

If you would like for Haste Server to start at boot, use the command:

sudo update-rc.d hasteserver defaults