diff --git a/Ubuntu-init.d-for-Haste-Server.md b/Ubuntu-init.d-for-Haste-Server.md new file mode 100644 index 0000000..14dd9f6 --- /dev/null +++ b/Ubuntu-init.d-for-Haste-Server.md @@ -0,0 +1,137 @@ +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 + # 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 + USER=root + + # Change the value of "DAEMON" to 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() + { + if [ ! -f "$LOCK_FILE" ] ; + then + echo `date` >> $LOG_FILE + echo " Starting Hastebin Server ...\n" | 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 + #[ $RETVAL -eq 0 ] && sudo touch $LOCK_FILE | tee -a $LOG_FILE + else + echo "$SERVER is locked. LOCK_FILE: $LOCK_FILE" | tee -a $LOG_FILE + RETVAL=1 + 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; + exit + 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" + exit + 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 /usr/lib/insserv/insserv hasteserver +