Skip to content

Instantly share code, notes, and snippets.

@Brant
Created June 24, 2014 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Brant/3c315504e1c98142f6a9 to your computer and use it in GitHub Desktop.
Save Brant/3c315504e1c98142f6a9 to your computer and use it in GitHub Desktop.
Forever, node.js, CentOS Service, Unprivileged User
#!/bin/sh
# chkconfig: 2345 85 15
# description: Startup script for project.
# exit on first error
set -e
# user running this script
_user="$(id -u -n)"
# points to the root for forever config
export "FOREVER_ROOT=/home/brant/.forever"
# commands to run on "start" (new line per command)
startup=(
"/home/brant/project/node_modules/forever/bin/forever --sourceDir /home/brant/project start projectScript.js"
)
# commands to run on "stop" (new line per command)
stopitems=(
"/home/brant/project/node_modules/forever/bin/forever stop projectScript.js"
)
# start function
do_start(){
if [ "$_user" == "brant" ]; then
echo "USER: IT'S BRANT!"
for i in "${startup[@]}"
do
$i
done
else
echo "USER: NOT BRANT!"
for i in "${startup[@]}"
do
su - brant -c "$i"
done
fi
}
# stop function
do_stop(){
if [ "$_user" == "brant" ]; then
echo "USER: IT'S BRANT!"
for i in "${stopitems[@]}"
do
$i
done
else
echo "USER: NOT BRANT!"
for i in "${stopitems[@]}"
do
su - brant -c "$i"
done
fi
}
# Decide what command is being called
case "$1" in
start)
echo "Starting Project..."
do_start
echo "done."
;;
stop)
echo "Stoping Project..."
do_stop
echo "done."
;;
restart)
echo "Restarting Project..."
do_stop
do_start
echo "done."
;;
*)
echo "Usage: project {start|stop|restart}" >&2
exit 3
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment