Skip to content

Instantly share code, notes, and snippets.

@janl
Last active August 6, 2018 06:51
Show Gist options
  • Save janl/b097f7a578ec07e4101c to your computer and use it in GitHub Desktop.
Save janl/b097f7a578ec07e4101c to your computer and use it in GitHub Desktop.
#!/bin/sh -e
# start:
# call node_modules/hoodie-server/bin/start manually
# pipe stdout & stderr into files
# send to bg
# capture pid
# write pid to pidfile
# stop
# read pidfile
# if no pidfile,
# say no process
# kill -INT pid # so couchdb can stop gracefully
# rm the pidfile
apphome=/home/hoodie/appname/
pidfile=$apphome/run/hoodie.pid
stdoutfile=$apphome/log/hoodie.stdout
stderrfile=$apphome/log/hoodie.stderr
hoodie_user=hoodie
mkdir -p $apphome/log
mkdir -p $apphome/run
cd $apphome
case "$1" in
start)
# if pidfile exists, report and exit
# expect user to clean up stale pidfile
if [ -f "$pidfile" ]; then
echo "Pidfile still exists: $pidfile:"
cat $pidfile
exit 2
fi
# the command
sudo -u $hoodie_user \
COUCH_URL=http://127.0.0.1:5984 \
HOODIE_ADMIN_USER=admin \
HOODIE_ADMIN_PASS="$HOODIE_ADMIN_PASS" \
HOODIE_BIND_ADDRESS="$HOODIE_BIND_ADDRESS" \
HOME=$apphome \
node node_modules/hoodie-server/bin/start \
1>>$stdoutfile \
2>>$stderrfile \
&
pid=$!
echo $pid > $pidfile
echo "Started."
;;
stop)
if [ ! -f "$pidfile" ]; then
echo "Pidfile $pidfile does not exist."
exit 3
fi
pid=$(<$pidfile)
kill -INT $pid
rm $pidfile
echo "Stopped."
;;
*)
echo "Invalid or missing command. Try 'start' or 'stop'."
;;
esac
@mikehedman
Copy link

For Hoodie to listen to external IP addresses, add the following line to the environment variables section (around line 40):
HOODIE_BIND_ADDRESS=0.0.0.0
(thanks @janl for pointing this out to me!)

@janl
Copy link
Author

janl commented Jun 15, 2015

Thanks, added!

I made it so that you can do HOODIE_BIND_ADDRESS=0.0.0.0 ./hoodie-daemon.sh

@mikehedman
Copy link

One other small change. This line:
pid=$(<$pidfile)
does not work on my Ubuntu server (no value is assigned to $pid), but this does:
pid=$(cat $pidfile)

@janl
Copy link
Author

janl commented Jun 15, 2015

this is standard bourne shell, should work on anything from the 90s, possibly earlier :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment