Skip to content

Instantly share code, notes, and snippets.

@kyleturman
Last active May 14, 2020 13:56
Show Gist options
  • Save kyleturman/7455452 to your computer and use it in GitHub Desktop.
Save kyleturman/7455452 to your computer and use it in GitHub Desktop.
Enable `sass --watch` as a daemonized service on Ubuntu & Debian.
#! /bin/sh -e
# Located in /etc/init.d/sass
DAEMON="/path/to/sass-watch"
DAEMONUSER="SOMEUSER"
DAEMON_NAME="sass-watch"
### BEGIN INIT INFO
# Provides: sass
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop Sass Watching
# Description: SassWatch
### END INIT INFO
PATH="/sbin:/bin:/usr/sbin:/usr/bin"
test -x $DAEMON || exit 0
. /lib/lsb/init-functions
d_start () {
log_daemon_msg "Starting system $DAEMON_NAME Daemon"
start-stop-daemon --background --name $DAEMON_NAME --start --user $DAEMONUSER --exec $DAEMON
log_end_msg $?
}
d_stop () {
log_daemon_msg "Stopping system $DAEMON_NAME Daemon"
start-stop-daemon --name $DAEMON_NAME --stop --retry 5 --name $DAEMON_NAME
kill $(ps aux | grep 'sass' | awk '{print $2}')
log_end_msg $?
}
case "$1" in
start|stop)
d_${1}
;;
restart|reload|force-reload)
d_stop
d_start
;;
force-stop)
d_stop
killall -q $DAEMON_NAME || true
sleep 2
killall -q -9 $DAEMON_NAME || true
kill $(ps aux | grep 'sass' | awk '{print $2}')
;;
status)
status_of_proc "$DAEMON_NAME" "$DAEMON" "system-wide $DAEMON_NAME" && exit 0 || exit $?
;;
*)
echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|force-stop|restart|reload|force-reload|status}"
exit 1
;;
esac
exit 0

This guide assumes that you have ruby and the sass gem installed, and that you have a basic knowledge of the unix terminal. I use nano here, but you can use whatever editor you'd like.

###Find out Path Info

$ echo $PATH
$ gem environment

Save output from $PATH variable and paths in "GEM PATHS:" in a blank text file somewhere for use in the sass-watch file.

###Make Files

$ nano /path/to/sass-watch
$ chomod 755 /path/to/sass-watch

Make sure you get >>> Sass is watching for changes. Press Ctrl-C to stop. whenever you you execute /path/to/sass-watch before making the daemon file.

$ sudo nano /etc/init.d/sass
$ sudo chmod 755 /etc/init.d/sass

Test and make sure /etc/init.d/sass start and /etc/init.d/sass stop work before adding to update-rc.

$ sudo update-rc.d sass defaults

###Try it Out

$ sudo service sass start
$ sudo service sass stop
#! /bin/sh -e
# Located in /path/to/sass-watch
export PATH="/OUTPUT/FROM/PATH"
export GEM_HOME="/OUTPUT/FROM/GEM/ENVIRONMENT/gems"
export GEM_PATH="/OUTPUT/FROM/GEM/ENVIRONMENT:/OUTPUT/FROM/GEM/ENVIRONMENT/gems"
sass --style compact --watch /path/to/www/
@misterhaan
Copy link

thanks! had to change the --user to --chuid to get it to run as my user, but then it worked great for me!

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