Running compass watch in the background

Since a long time ago, i wanna write a script to auto compile sass -> css. A slight disadvantage (besides of having a ton of advantages) of using Sass over native CSS is that you need to compile your Sass files to CSS before loading them up in your browser by compass command.

In bash shell script, the ampersand “&” is used to fork processes and runn in the background:

compass watch [path/to/project] &

Will cause the find command to be forked and run in the background (you can always kill it by it’s PID)

The problem with using the & (ampersand) for forking a process in the shell is that whenever you close the shell the process is going to be killed because the parent process is killed.

& runs the whole thing in the background, giving you your prompt back immediately.

nohup allows the background process to continue running even after the user logs out (or exits the initiating shell).

nohup compass watch &

Explanation:

Every Linux process opens three I/O channels, an input “stdin”, a standard output “stdout” and a standard error output “stderr”. They can be used for binary but are traditionally text. When most programs see stdin close, they exit (this can be changed by the programmer).

When the parent shell exits, stdin is closed on the children, and (often, usually) the children exit as well. In addition the children receive a software signal, SIGHUP, indicating the user has “hung up” (formerly, the modem) and the default here is to exit as well. (Note, a programmer can change all of this when writing the program).

So, what nohup does is give the child process a separate I/O environment, tying up the ins and outs to something not tied to the parent shell, and shielding the child from the SIGHUP signal. Once the user disconnects, you will see the nohup background process owned by init (process 1), not the user’s shell.

Save following script as ~/bin/sass-watch and chmod +x for x permission.

cmd_dir=`pwd`
pidfile="$cmd_dir/.sass.pid"
logfile="$cmd_dir/.sass.log"
if [ -f $pidfile ]; then
    kill -9 `cat $pidfile` >/dev/null 2>&1
    [ "$1" = "stop" ] && echo 'shutdown!' && exit 0;
fi
nohup compass watch>$logfile 2>&1&
echo $! >$pidfile
echo "compass watch success, pid: $!";

Also the final script is https://gist.github.com/allex/965f3e1a0876592db33f

Run sass-watch on your sass project for launch daemon.

Some helper reference at internal variables

$! represents the PID of the last process executed.

$$ means the process ID that the script file is running under. For any given script, when it is run, it will have only one “main” process ID. Regardless of how many subshells you invoke, $$ will always return the first process ID associated with the script.

2 thoughts on “Running compass watch in the background

  1. gnp

    Hi,

    Thanks for this, very useful.
    However, I have one issue even after I using this, that when ever facing any error in scss, the process is stopped and showing error.
    Is any possible to skip error and continue watch?

    Thanks,
    GNP.

    1. Allex Wang Post author

      Actually the watcher still working even if it has errors. you can tail -f .sass.log for details, if still halt, check your compass version.

Comments are closed.