Get Grunt to Watch SASS with Native Job Control
Grunt is a pretty handy tool when it comes to compiling your sass. If you like
to use Foundation 5 the cli will create a Gruntfile for you when you create a
new project. There is a myriad of ways to make Grunt watch you sass but I also
like terminal for vim so I want my terminal to still be available when I run
grunt watch
. I can always just open second terminal window and run:
$ grunt watch
which works fine but we want better than just fine, we want to be terminal ninjas. Were not going to install anything new either because we already have everything we need built in to our Unix/Linux system. Using the build in job control we are going to run this command to have grunt run.
$ grunt watch > /dev/null 2>&1 &
Looks really fancy but I’ll break it down. First is the > /dev/null 2>&1
part. This is simply redirecting messages from stdout(standard out) and
stderr(standard error) to /dev/null. It’s a special place that discards
everything written to it, essentially you are hiding the messages output by
grunt so they won’t pop up in your terminal. You don’t have to write to
/dev/null either you could replace it with a filename such as:
$ grunt watch > grunt.log 2>&1 &
So that you could actually check the file grunt.log whenever you want for the
output of the command grunt watch
. The second part of the original command is
the &
at the very end. When you run a command with a trailing ampersand it
will run that command in the background of your terminal. You can confirm the
command is running at any time by typing:
$ jobs
[1]+ Running grunt watch > /dev/null 2>&1 & (wd: ~/Sites/foundation-test)
The jobs
command will display all the currently running jobs. To bring that
job back to the foreground of your terminal simply type fg
or fg %#
where #
is the job number, if you are running multiple jobs at the same time. You can
always kill the currently job with Ctrl+c or put it to sleep with Ctrl+z. A
sleeping process can then be put into the background with bg
or bg %#
. The
built in job control in Unix/Linux is more than sufficient for getting Grunt to
watch sass, at least in my circumstances. If you need even more flexibility you
can search out other solutions but I will just stick with standard job control
for now.