Skip to content

Instantly share code, notes, and snippets.

@galiazzi
Last active May 7, 2018 12:31
Show Gist options
  • Save galiazzi/f10014ba91013f06c79af19195585c8f to your computer and use it in GitHub Desktop.
Save galiazzi/f10014ba91013f06c79af19195585c8f to your computer and use it in GitHub Desktop.
Configuração de um serviço no linux utilizando forever
#!/bin/bash
#
# description: DevShelf service
# processname: node
# pidfile: /var/run/devshelf.pid
# logfile: /var/log/devshelf.log
#
# Based on https://gist.github.com/jinze/3748766
#
# To use it as service on Ubuntu:
# sudo cp devshelf.sh /etc/init.d/devshelf
# sudo chmod a+x /etc/init.d/devshelf
# sudo update-rc.d devshelf defaults
#
# Then use commands:
# service devshelf <command (start|stop|etc)>
NAME=watson-server # Unique name for the application
SOUREC_DIR=/home/apps/watson-server # Location of the application source
COMMAND=node # Command to run
SOURCE_NAME=app.js # Name os the applcation entry point script
USER=root # User for process running
NODE_ENVIROMENT=production # Node environment
pidfile=/var/run/$NAME.pid
logfile=/var/log/$NAME.log
forever=forever
start() {
export NODE_ENV=$NODE_ENVIROMENT
echo "Starting $NAME node instance : "
touch $logfile
chown $USER $logfile
touch $pidfile
chown $USER $pidfile
# iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
sudo -H -u $USER $forever start --id=$NAME --pidFile $pidfile -l $logfile -a --sourceDir $SOUREC_DIR -c $COMMAND $SOURCE_NAME
RETVAL=$?
}
restart() {
echo -n "Restarting $NAME node instance : "
sudo -H -u $USER $forever restart $NAME
RETVAL=$?
}
status() {
echo "Status for $NAME:"
sudo -H -u $USER $forever list
RETVAL=$?
}
stop() {
echo -n "Shutting down $NAME node instance : "
sudo -H -u $USER $forever stop $NAME
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
*)
echo "Usage: {start|stop|status|restart}"
exit 1
;;
esac
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment